【发布时间】:2021-01-17 15:55:14
【问题描述】:
我想读取文本文件的位置,因此当我重新打开表单时,它应该位于我上次关闭它的位置。有什么办法吗?
我要写入文本文件的代码:
string path = Directory.GetCurrentDirectory() + @"\config\Loc\dateloc.txt";
if (!File.Exists(path))
{
File.Create(path);
}
try
{
System.IO.File.WriteAllBytes(path, new byte[0]);
StreamWriter writer = new StreamWriter(path, true);
string overlayloc = this.Location.ToString();
writer.WriteLine(overlayloc.ToString());
writer.Dispose();
writer.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
【问题讨论】:
-
你为什么开始在那个文件中写入一个字节?如果文件不存在,StreamWriter 也会创建该文件。最后,您以附加模式写入,您在该文件中写入的位置是否比当前表单中的位置更多?
-
嗯,首先你必须确定\澄清这个设置是应用程序、机器还是用户特定的,然后是控制特定的。本质上,什么范围?
-
visual-studio-2019← 除非您对此特定版本有特定疑问,否则请勿使用此标签。 -
使用应用程序设置和 Visual Studio RAD 设计器的 CodeProject 教程:Windows Forms User Settings in C# 和 Restore Form Position and Size in C#