TheWalkingshadow
TheWalkingshadow

Reputation: 1

Write File with text from resources visual basic

I'm writing some program in VB, and I want to create txt file with text from file in resources. You didn't understood, did you? So, it goes like this.

 Dim path As String = "c:\temp\MyTest.txt" 

    ' Create or overwrite the file. 
    Dim fs As FileStream = File.Create(path)

    ' Add text to the file. 
    Dim info As Byte() = New UTF8Encoding(True).GetBytes("This is some text in the file.")
    fs.Write(info, 0, info.Length)
    fs.Close()

is code for creating txt file with certain text. But, I need the following.

 Dim fd As New FolderBrowserDialog
    fd.ShowDialog()

is the only function that I have in program, and, when folder is selected, I need to create file in that folder, file's name should be config.cfg, but, text in file which is going to be created in selected folder should be text from mine txt file which is in Recources.

I've tried

 Dim path As String = fd.SelectedPath 
    Dim fs As FileStream = File.Create(path)

    ' upisuje tekst u fajl 
    Dim info As Byte() = New UTF8Encoding(True).GetBytes(application.startuppath &  "\..\..\Resources\config.cfg")
    fs.Write(info, 0, info.Length)
    fs.Close()

but the text I got in file is directory from where is my program debugged. Any ideas to do this? :)

Upvotes: 0

Views: 1570

Answers (1)

LarsTech
LarsTech

Reputation: 81610

If you added a text file to your resources, then you can try something like this:

Using fbd As New FolderBrowserDialog
  If fbd.ShowDialog(Me) = DialogResult.OK Then
    File.WriteAllText(Path.Combine(fbd.SelectedPath, "config.cfg"), My.Resources.config)
  End If
End Using

The file I added was called config, and it made a config.txt file in my resource library.

Upvotes: 1

Related Questions