1

So I am new to c# and WinForms and I am trying to save the contents of a textbox, all of the things I saw online were completely useless and to complete my project i need to be able to save the contents of a textbox between sessions.

Here is my code:

namespace Zero_Light_Fullstack
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string link1 = textBox1.Text;
                Process.Start("chrome", @link1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
    }
}

Sorry if this is messy any answer is appreciated!

Lochyj
  • 63
  • 7
  • 1
    https://stackoverflow.com/questions/3032492/save-settings-in-a-net-winforms-application or to a file: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-read-from-a-text-file#:~:text=%20How%20to%20read%20from%20a%20text%20file,the%20specified...%205%20See%20also.%20%20More%20 – T.Schwarz Jun 15 '21 at 07:28
  • `System.IO.File.WriteAllText(path, textBox1.Text);` – Heslacher Jun 15 '21 at 07:32
  • Or automatic, using ApplicationSettings' PropertyBinding, as described [here](https://stackoverflow.com/a/61160815/7444103) (of course, related to the `Text` Property) – Jimi Jun 15 '21 at 08:03

1 Answers1

0

Does it need to be to a file?

You could create a string Setting in the application and save/access it when you like.

enter image description here

Accessing a setting.

textBox1.Text = Zero_Light_Fullstack.Properties.Settings.Default.LastText;

Updating a setting.

Zero_Light_Fullstack.Properties.Settings.Default.LastText = textBox1.Text;
Zero_Light_Fullstack.Properties.Settings.Default.Save();