943,147 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 3644
  • VB.NET RSS
Mar 17th, 2010
0

TextBox save to txt file without using the save file dialog

Expand Post »
Hi

I am attempting to write a playlist for my media player which is already built. I have found a way to build a playlist for it but it would be helpful if I could save the contents of TextBox1 to a text file without using the save as. I am trying to get over a user inputting a file name for the finished text file so is it possible to write the file c:\test.txt without using the save as dialog?
I have found that I can use a vbs program file which creates a playlist in a folder. I save the vbs file text to the textbox then save it as a vbs file in the playlist directory, then execute the finished file to make the playlist.
Any help please.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jakewebb is offline Offline
4 posts
since Mar 2010
Mar 17th, 2010
0
Re: TextBox save to txt file without using the save file dialog
You can use static methods of the class File to create a file and write text. You can also create an instance of the class StreamWriter to create and write to a file. Both are in the System.IO namespace.

Here is a usage example of both.

VB.NET Syntax (Toggle Plain Text)
  1. Imports System.IO
  2.  
  3. Module Module1
  4.  
  5. Sub Main()
  6.  
  7. Dim fileName1 As String = "C:\Temp\filedemo_streamwriter.txt"
  8.  
  9. Using writer As New StreamWriter(fileName1)
  10. writer.Write("This is a demo using a StreamWriter object.")
  11. writer.Close()
  12. End Using
  13.  
  14.  
  15. Dim fileName2 As String = "C:\Temp\filedemo_file.txt"
  16. File.WriteAllText(fileName2, "This is a demo using the File class")
  17.  
  18. End Sub
  19.  
  20. End Module
Sponsor
Reputation Points: 318
Solved Threads: 135
LINQ!
apegram is offline Offline
550 posts
since Jan 2010
Mar 17th, 2010
0

Thanks for your help

Thank you for your help it will help me to work out what is happening I do like to try first, this question was asked afer about 20 hours of trying myself so I am grateful. I will play around with this code sample in the next few hours when my youngster is in bed and all is quiet.
Bill
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jakewebb is offline Offline
4 posts
since Mar 2010
Mar 17th, 2010
0

How do I link to the module?

Hi sorry I am unable to work this out still. I have entered the code into the module but I am having trouble working out how to call it. I am a self learner and have not learned how to call from a module in the way required here. I did a previous project with "sleep" in the module but that was different. Could you tell me how to call the module code please? If you wish to see the project I have made for my friend Pete the player is here.
http://homepage.ntlworld.com/c.rover/PetesPlayer.zip
Sorry if its amaturish I am not that brilliant at this yet. Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jakewebb is offline Offline
4 posts
since Mar 2010
Mar 17th, 2010
0
Re: TextBox save to txt file without using the save file dialog
Put a button on your form. Click it in the designer view so it will generate code for a button click event. Take the code I wrote (between the lines Sub Main() and End Sub) and drop it into the click handler and modify it to meet your needs.
Sponsor
Reputation Points: 318
Solved Threads: 135
LINQ!
apegram is offline Offline
550 posts
since Jan 2010
Mar 18th, 2010
0

Solved many many thanks to you sir

Hi after a nights sleep ive got it working thanks to you. I am now a big step closer to making my playlist for my player here is what ive done, or rather what you did most of for me.
VB.NET Syntax (Toggle Plain Text)
  1. Dim fileName1 As String = "C:\filedemo_streamwriter.vbs"
  2. Using writer As New IO.StreamWriter(fileName1)
  3. 'writer.Write("This is a demo using a StreamWriter object.")
  4. writer.Write(TextBox1a.Text)
  5. writer.Close()
  6. End Using
  7. Dim fileName2 As String = "C:\filedemo_file.vbs"
  8. IO.File.WriteAllText(fileName2, "This is a demo using the File class")
  9. End Sub
This is the vbs file that your code now writes into a directory which makes the playlist.
VB.NET Syntax (Toggle Plain Text)
  1. m3uName = fdr.ParentFolder.Name & " (" & fdr.Name & ").m3u"
  2. else
  3. m3uName = fdr.Name & ".m3u"
  4. end if
  5.  
  6. ' Existing m3u file handling
  7. m3u = path & "\" & m3uName
  8. if fso.FileExists(m3u) then
  9. if delete then
  10. wscript.echo "... deleting existing file"
  11. fso.DeleteFile m3u
  12. else
  13. wscript.echo "... renaming existing file"
  14. fso.MoveFile m3u, m3u & ".old"
  15. end if
  16. end if
  17.  
  18. ' Write new m3u file
  19. wscript.echo "... writing """ & m3uName & """"
  20. set m3uFile = fso.OpenTextFile(m3u, ForWriting, True)
  21. m3uFile.Write(mp3List)
  22. m3uFile.Close
  23. count = 1
  24. else
  25. wscript.echo "... no mp3/wma files found"
  26. end if
  27. end if
  28.  
  29. ' Return m3u file count
  30. WriteM3u = count
  31. end function
I am gradualy adding more file extensions to it. This may seem a long way round about making a playlist but as you have gathered I am not that brilliant with code and this stuff realy does help me to learn. Besides I love trying stuff. Regards Bill
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jakewebb is offline Offline
4 posts
since Mar 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: log in program
Next Thread in VB.NET Forum Timeline: Help to delete an entry in a database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC