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.

Recommended Answers

All 5 Replies

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.

Imports System.IO

Module Module1

    Sub Main()

        Dim fileName1 As String = "C:\Temp\filedemo_streamwriter.txt"

        Using writer As New StreamWriter(fileName1)
            writer.Write("This is a demo using a StreamWriter object.")
            writer.Close()
        End Using


        Dim fileName2 As String = "C:\Temp\filedemo_file.txt"
        File.WriteAllText(fileName2, "This is a demo using the File class")

    End Sub

End Module

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

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.

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.

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.

Dim fileName1 As String = "C:\filedemo_streamwriter.vbs"
        Using writer As New IO.StreamWriter(fileName1)
            'writer.Write("This is a demo using a StreamWriter object.")
            writer.Write(TextBox1a.Text)
            writer.Close()
        End Using
        Dim fileName2 As String = "C:\filedemo_file.vbs"
        IO.File.WriteAllText(fileName2, "This is a demo using the File class")
    End Sub

This is the vbs file that your code now writes into a directory which makes the playlist.

m3uName = fdr.ParentFolder.Name & " (" & fdr.Name & ").m3u"
else
m3uName = fdr.Name & ".m3u"
end if

' Existing m3u file handling
m3u = path & "\" & m3uName
if fso.FileExists(m3u) then
if delete then
wscript.echo "... deleting existing file"
fso.DeleteFile m3u
else
wscript.echo "... renaming existing file"
fso.MoveFile m3u, m3u & ".old"
end if
end if

' Write new m3u file
wscript.echo "... writing """ & m3uName & """"
set m3uFile = fso.OpenTextFile(m3u, ForWriting, True)
m3uFile.Write(mp3List)
m3uFile.Close
count = 1
else
wscript.echo "... no mp3/wma files found"
end if
end if

' Return m3u file count
WriteM3u = count
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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.