当表单打开时,如何保留上次使用的文件夹并将其放在文本框中?

问题描述 投票:0回答:1

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

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

我该怎么做?

c# forms path textbox application-settings
1个回答
0
投票

在解决方案资源管理器中,双击项目Settings.settings部分中的Properties

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

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

如果您选择将其设置为空(建议),请在静态Program类中添加以下变量:

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

Main方法的开头添加:

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

现在获取它,您可以在表单的FormLoad事件中编写:

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

放入FormClosed事件:

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

这里是您的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);
      }
    }

  }

}

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

  }

}
© www.soinside.com 2019 - 2024. All rights reserved.