0

When the C# form is loaded, I want to have a folder in a textbox belonging to the user who is logged on by default.

I have textbox and buttons for an input and output file, and when the form is opened I want the user to see the default folder where he saved the previous output file.

How can I do this?

Zeynep Baran
  • 37
  • 1
  • 7
  • 3
    [Using Application Settings and User Settings](https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/using-application-settings-and-user-settings) – 001 Oct 02 '19 at 13:05

1 Answers1

1

Double click on the Settings.settings in the Properties section of the project in the solution explorer.

It opens the parameters wizard containing a grid view.

At bottom, add a parameter named for example LastPath and set it to string type and choose a default value that can be empty.

If you choose to set it empty (recommended), add these variables in the static Program class:

    static string UserDataFolderPath
      = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
      + Path.DirectorySeparatorChar
      + AssemblyCompany
      + Path.DirectorySeparatorChar
      + AssemblyTitle
      + Path.DirectorySeparatorChar;

    static string UserDocumentsFolderPath
      = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
      + Path.DirectorySeparatorChar
      + AssemblyCompany
      + Path.DirectorySeparatorChar
      + AssemblyTitle
      + Path.DirectorySeparatorChar;

And these methods:

    static public string AssemblyCompany
    {
      get
      {
        object[] attributes = Assembly.GetExecutingAssembly()
                              .GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
        if ( attributes.Length == 0 )
        {
          return "";
        }
        return ( (AssemblyCompanyAttribute)attributes[0] ).Company;
      }
    }

    static public string AssemblyTitle
    {
      get
      {
        object[] attributes = Assembly.GetExecutingAssembly()
                              .GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
        if ( attributes.Length > 0 )
        {
          AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
          if ( titleAttribute.Title != "" )
          {
            return titleAttribute.Title;
          }
        }
        return Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
      }
    }

Add at the beginning of the Main method:

Directory.CreateDirectory(UserDataFolderPath);
Directory.CreateDirectory(UserDocumentsFolderPath);
if ( Properties.Settings.Default.LastPath == "" )
{
  Properties.Settings.Default.LastPath = UserDataFolderPath;
  Properties.Settings.Default.Save();
}

Now to get it you can write in the FormLoad event of your form:

textBox1.Text = Properties.Settings.Default.LastPath;

Put in the FormClosed event:

Properties.Settings.Default.LastPath = textBox1.Text;
Properties.Settings.Default.Save();

Here what should be your Program.cs:

using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace WindowsFormsAppTest
{

  static class Program
  {

    [STAThread]
    static void Main()
    {
      Directory.CreateDirectory(UserDataFolderPath);
      Directory.CreateDirectory(UserDocumentsFolderPath);
      if ( Properties.Settings.Default.LastPath == "" )
      {
        Properties.Settings.Default.LastPath = UserDataFolderPath;
        Properties.Settings.Default.Save();
      }
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.Run(new FormTest());
    }

    static string UserDataFolderPath
      = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
      + Path.DirectorySeparatorChar
      + AssemblyCompany
      + Path.DirectorySeparatorChar
      + AssemblyTitle
      + Path.DirectorySeparatorChar;

    static string UserDocumentsFolderPath
      = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
      + Path.DirectorySeparatorChar
      + AssemblyCompany
      + Path.DirectorySeparatorChar
      + AssemblyTitle
      + Path.DirectorySeparatorChar;

    static public string AssemblyCompany
    {
      get
      {
        object[] attributes = Assembly.GetExecutingAssembly()
                              .GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
        if ( attributes.Length == 0 )
        {
          return "";
        }
        return ( (AssemblyCompanyAttribute)attributes[0] ).Company;
      }
    }

    static public string AssemblyTitle
    {
      get
      {
        object[] attributes = Assembly.GetExecutingAssembly()
                              .GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
        if ( attributes.Length > 0 )
        {
          AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0];
          if ( titleAttribute.Title != "" )
          {
            return titleAttribute.Title;
          }
        }
        return Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
      }
    }

  }

}

And your Form.cs:

using System;
using System.Windows.Forms;

namespace WindowsFormsAppTest
{

  public partial class FormTest : Form
  {

    public FormTest()
    {
      InitializeComponent();
    }

    private void FormTest_Load(object sender, EventArgs e)
    {
      textBox1.Text = Properties.Settings.Default.LastPath;
    }

    private void FormTest_FormClosed(object sender, FormClosedEventArgs e)
    {
      Properties.Settings.Default.LastPath = textBox1.Text;
      Properties.Settings.Default.Save();
    }

  }

}
  • I did this. But textbox was empty again.I've done a definition like this in Setting.Settings, is it correct? public string LastPath = ""; – Zeynep Baran Oct 03 '19 at 09:27
  • You must not add any code in the `Settings.Designer.cs` but you can add parameters using the VS wizard: I updated the asnwer. –  Oct 03 '19 at 09:46
  • Directory.CreateDirectory(UserDataFolderPath); Directory.CreateDirectory(UserDocumentsFolderPath); here it gave an error in expressions in parentheses. Saying it was compiled before. – Zeynep Baran Oct 03 '19 at 11:20
  • I don't understand but I see I forgot some `static` clauses. I updated the answer with full code. –  Oct 03 '19 at 11:31
  • Thank you so much. My fault, I made the wrong naming. Tahnk you for help. @Oliver Rogier – Zeynep Baran Oct 03 '19 at 13:42