Search code examples
c#saveopenfiledialog

Save user input


So basically, I have a OpenFileDialog fully working which displays the chosen dir in a textbox. But I'm wondering how would I save the users input so when they would restart the application it would stay in the textbox? so they wouldn't have to do it over every time. I understand this might be a stupid question, but I've been googeling some time and found nothing like this. Thanks.


Solution

  • public Form1()
        {
            InitializeComponent();
            if(File.Exists(@"path.txt"))
                textBox1.Text = File.ReadAllText(@"path.txt");
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            if(File.Exists(@"path.txt") == false)
                File.Create(@"path.txt");
            File.WriteAllText(@"path.txt", textBox1.Text);
        }
    

    Just write it in a file, don't forget to include System.IO in your references.