How to save text box data in a text file using visual studio


  • I am typing some values in a text box which is on GUI. Then I want save those data in a text file. How i do it. Please someone help me. 

    Saturday, October 22, 2016 7:51 PM

All replies

  • Hi,


        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            'change the path to the file
            IO.File.WriteAllText("C:\users\...\documents\dajd32178.txt", TextBox1.Text)
        End Sub



    Saturday, October 22, 2016 8:10 PM
  • In C#, you could write the entire contents of a text box to a file like this:

    System.IO.File.WriteAllText(fileName, values);

    In this case, the fileName is a variable that holds the path and the file name while values is a variable that holds the text that needs to go into the given file. This is a general purpose example and it doesn't matter whether the actual value of the values variable is hard-coded, comes from a Windows Forms text box, ASP.NET text box, or from some other source.

    If you need this kind of functionality in a Windows Form, your code may look like this:

    private void btnSave_Click(object sender, EventArgs e)
    	var dialogResult = sfdSaveValuesAsFile.ShowDialog();
    	if(dialogResult == System.Windows.Forms.DialogResult.OK)
    		System.IO.File.WriteAllText(sfdSaveValuesAsFile.FileName, txtValues.Text);

    In the above example code, the file name along with the path is coming from the SaveFileDialog component which you can find in the Windows Forms toolbox. It will show a Save File Dialog window to the user and ask him to select a folder and enter a file name before the value of the txtValues text box is saved into the new file.

    Please note that calling WriteAllText method of the File class is the easiest way to save text into a file. However, there other classes in the System.IO namespace that you can use to write text or even bytes to files.

    • Proposed as answer by awesomecode Saturday, October 22, 2016 8:44 PM
    Saturday, October 22, 2016 8:40 PM
  • Hi dumiz,

    Welcome to the MSDN forum.

    Sorry to trouble you and I just want to confirm that does your issue is solved or not?

    If you want to use the VB or C# to develop this, you can see the reply from Thorsten Gudera (VB language) and awesomecode (C# language).

    If you want use other development language to implement, please feel free to let me know, I will provide the sample code for your requirement.

    Best regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Monday, October 24, 2016 5:34 AM