打开表单时,如何保留上次使用的文件夹并将其放在文本框中? [英] How to keep the last used folder and put it in a textbox when a form opens?
问题描述
加载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();
}
}
}
这篇关于打开表单时,如何保留上次使用的文件夹并将其放在文本框中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!