C# 当表单打开时,如何保留最后使用的文件夹并将其放入文本框中?

C# 当表单打开时,如何保留最后使用的文件夹并将其放入文本框中?,c#,forms,path,textbox,application-settings,C#,Forms,Path,Textbox,Application Settings,加载C表单时,我希望在文本框中有一个属于默认登录用户的文件夹 我有一个输入和输出文件的文本框和按钮,当表单打开时,我希望用户看到他保存前一个输出文件的默认文件夹 如何执行此操作?双击解决方案资源管理器中项目属性部分的Settings.Settings 它将打开包含栅格视图的参数向导 在底部,添加一个名为example LastPath的参数,并将其设置为string类型,然后选择一个可以为空的默认值 如果选择将其设置为空,请在静态程序类中添加以下变量: static string Use

加载C表单时,我希望在文本框中有一个属于默认登录用户的文件夹

我有一个输入和输出文件的文本框和按钮,当表单打开时,我希望用户看到他保存前一个输出文件的默认文件夹

如何执行此操作?

双击解决方案资源管理器中项目属性部分的Settings.Settings

它将打开包含栅格视图的参数向导

在底部,添加一个名为example LastPath的参数,并将其设置为string类型,然后选择一个可以为空的默认值

如果选择将其设置为空,请在静态程序类中添加以下变量:

    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);
      }
    }
在主方法的开头添加:

现在,您可以在表单的FormLoad事件中写入:

textBox1.Text = Properties.Settings.Default.LastPath;
以关闭事件的形式输入:

以下是您的程序。cs:

和您的Form.cs:


是我干的。但文本框又是空的。我在设置中做了这样的定义。设置,正确吗?公共字符串LastPath=;您不能在Settings.Designer.cs中添加任何代码,但可以使用VS向导添加参数:我更新了asnwer.Directory.CreateDirectoryUserDataFolderPath;Directory.CreateDirectoryUserDocumentsFolderPath;在这里,它在括号中的表达式中给出了一个错误。说它是以前编译过的。我不明白,但我发现我忘记了一些静态子句。我用完整的代码更新了答案。非常感谢。是我的错,我取错了名字。谢谢你的帮助@奥利弗·罗吉尔
textBox1.Text = Properties.Settings.Default.LastPath;
Properties.Settings.Default.LastPath = textBox1.Text;
Properties.Settings.Default.Save();
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);
      }
    }

  }

}
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();
    }

  }

}