Member Avatar for Member #917687

Hello all, I just joined the site.

I hope someone can help me with a minor problem, I have been searching both this site and google for the anwser... but keep getting stuck.

A dialog with a textbox is reading a text file within the project like this

Private Sub StandardPharses_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim mystream As System.IO.Stream = System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream("Textfile1.txt")
        Using reader As New IO.StreamReader(mystream)
            ' Read the contents into TextBox
            TextBox1.Text = reader.ReadToEnd
        End Using
    End Sub

That works fine, but how to save the edited text back to the file when I hit Save

Dani AI

Generated

The file in the original load code is coming from an embedded resource inside the assembly. As pointed out, embedded resources are baked into the EXE and cannot be rewritten at runtime. To make the text editable and savable use a file on disk instead.

Two simple, practical approaches:

  • Add the text file to the project as Build Action = Content and set "Copy to Output Directory" = "Copy if newer" so a loose Textfile1.txt sits next to the EXE (good for testing).
  • Better: deploy a default file (either as Content or embedded) but on first run copy that default into a per-user writable folder (ApplicationData) and then always read/write that copy. This avoids permission problems when the app is installed under Program Files.

Example (VB.NET) — create a writable path, load from it if present, and save back to it:

Private Function GetUserFilePath() As String
    Dim appFolder = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyAppName")
    IO.Directory.CreateDirectory(appFolder)
    Return IO.Path.Combine(appFolder, "Textfile1.txt")
End Function

' Load (call on form load)
If IO.File.Exists(GetUserFilePath()) Then
    TextBox1.Text = IO.File.ReadAllText(GetUserFilePath(), Text.Encoding.UTF8)
End If

' Save (call from Save button)
Try
    IO.File.WriteAllText(GetUserFilePath(), TextBox1.Text, Text.Encoding.UTF8)
    MessageBox.Show("Saved.")
Catch ex As Exception
    MessageBox.Show("Save failed: " & ex.Message)
End Try

Notes and troubleshooting: File.WriteAllText overwrites an existing file (no need to delete first, contrary to ’s suggestion). If saving fails check for UnauthorizedAccessException (writing to Program Files requires elevation), ensure the file is actually copied as Content during build, and verify the path used. For multi-user scenarios prefer SpecialFolder.CommonApplicationData or per-user ApplicationData so each user has a writable copy.

Recommended Answers

All 4 Replies

you want to update the same file? first delete the content and using io.write u can write back and save...

i believe you can't possibly write back since the file you are using is read-only. It must be embedded into the exe(Check if I am right). You can embed files only before compiling or if you could find a way to manipulate an exe but that will just corrupt the exe or may produce runtime inconsistency, in other words not-poossible. rather store data in .config files

Also System.IO helps writing to files on the disk ur file is probably not stored on disk :) so not io functionality can work

Member Avatar for Member #917687

you want to update the same file? first delete the content and using io.write u can write back and save...

Thanks for your reply, can you provide me with an example code? as I have tried something like that with no luck

Member Avatar for Member #917687

i believe you can't possibly write back since the file you are using is read-only. It must be embedded into the exe(Check if I am right). You can embed files only before compiling or if you could find a way to manipulate an exe but that will just corrupt the exe or may produce runtime inconsistency, in other words not-poossible. rather store data in .config files

Also System.IO helps writing to files on the disk ur file is probably not stored on disk :) so not io functionality can work

Thanks for your reply.. yeah you might be right about the read only.. I will look into useing a .config insted

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.