<bdo id='AYBXF'></bdo><ul id='AYBXF'></ul>
    <i id='AYBXF'><tr id='AYBXF'><dt id='AYBXF'><q id='AYBXF'><span id='AYBXF'><b id='AYBXF'><form id='AYBXF'><ins id='AYBXF'></ins><ul id='AYBXF'></ul><sub id='AYBXF'></sub></form><legend id='AYBXF'></legend><bdo id='AYBXF'><pre id='AYBXF'><center id='AYBXF'></center></pre></bdo></b><th id='AYBXF'></th></span></q></dt></tr></i><div id='AYBXF'><tfoot id='AYBXF'></tfoot><dl id='AYBXF'><fieldset id='AYBXF'></fieldset></dl></div>

    <tfoot id='AYBXF'></tfoot>

    <small id='AYBXF'></small><noframes id='AYBXF'>

    <legend id='AYBXF'><style id='AYBXF'><dir id='AYBXF'><q id='AYBXF'></q></dir></style></legend>

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

      时间:2023-06-10
      <legend id='LOfYO'><style id='LOfYO'><dir id='LOfYO'><q id='LOfYO'></q></dir></style></legend>
      <tfoot id='LOfYO'></tfoot>

          <tbody id='LOfYO'></tbody>

          <small id='LOfYO'></small><noframes id='LOfYO'>

              <bdo id='LOfYO'></bdo><ul id='LOfYO'></ul>
                <i id='LOfYO'><tr id='LOfYO'><dt id='LOfYO'><q id='LOfYO'><span id='LOfYO'><b id='LOfYO'><form id='LOfYO'><ins id='LOfYO'></ins><ul id='LOfYO'></ul><sub id='LOfYO'></sub></form><legend id='LOfYO'></legend><bdo id='LOfYO'><pre id='LOfYO'><center id='LOfYO'></center></pre></bdo></b><th id='LOfYO'></th></span></q></dt></tr></i><div id='LOfYO'><tfoot id='LOfYO'></tfoot><dl id='LOfYO'><fieldset id='LOfYO'></fieldset></dl></div>
              • 本文介绍了表单打开时如何保留最后使用的文件夹并将其放入文本框中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

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

                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.

                我该怎么做?

                推荐答案

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

                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.

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

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

                如果您选择将其设置为空(推荐),请在静态 Program 类中添加这些变量:

                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;
                

                还有这些方法:

                    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 事件中编写:

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

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

                这篇关于表单打开时如何保留最后使用的文件夹并将其放入文本框中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 Visual C# 中更改另一个窗体上的文本框中的文本? 下一篇:C# 调整文本框大小以适应内容

                相关文章

                <legend id='xKkvt'><style id='xKkvt'><dir id='xKkvt'><q id='xKkvt'></q></dir></style></legend>
              • <i id='xKkvt'><tr id='xKkvt'><dt id='xKkvt'><q id='xKkvt'><span id='xKkvt'><b id='xKkvt'><form id='xKkvt'><ins id='xKkvt'></ins><ul id='xKkvt'></ul><sub id='xKkvt'></sub></form><legend id='xKkvt'></legend><bdo id='xKkvt'><pre id='xKkvt'><center id='xKkvt'></center></pre></bdo></b><th id='xKkvt'></th></span></q></dt></tr></i><div id='xKkvt'><tfoot id='xKkvt'></tfoot><dl id='xKkvt'><fieldset id='xKkvt'></fieldset></dl></div>
                  <bdo id='xKkvt'></bdo><ul id='xKkvt'></ul>

                1. <small id='xKkvt'></small><noframes id='xKkvt'>

                2. <tfoot id='xKkvt'></tfoot>