【发布时间】:2015-07-20 18:20:58
【问题描述】:
我正在为我管理的 DayZ 服务器编写一个简单的启动器,其中一个先决条件是用户需要选择他们的 Arma 可执行文件的存储位置,唯一的问题是,在他们关闭程序后,设置会丢失,下次启动时需要重新配置。查找和接收路径名的代码如下:
public void setArma2PathToolStripMenuItem_Click(object sender, EventArgs e)
{
//Initialise Open File Dialogue
OpenFileDialog a2Path = new OpenFileDialog();
//Allow all files (although user will select .exe)
a2Path.Filter = "All Files (*.*)|*.*";
//if the path is legit
if (a2Path.ShowDialog() == DialogResult.OK)
{
//get the directory and store it to a2Directory, same with a2OA and a3
a2DirectoryPath = a2Path.FileName;
a2DirectoryPathModFriendly = Path.GetDirectoryName(a2Path.FileName);
//and tell the user it's been set
string a2DirectoryPathString = string.Format("Arma 2 Path set to: {0}", a2DirectoryPath);
MessageBox.Show(a2DirectoryPathString, "Arma 2 Path", MessageBoxButtons.OK);
}
}
ModFriendly 位是由于 arma 处理启动参数的方式。但这是我要保存的部分,以便可以通过这段代码调用。
public void launchArma2EpochChernarus_Click(object sender, EventArgs e)
{
string modsA2 = "@DayZ_Epoch";
string ipA2 = "";
int portA2 = ;
ProcessStartInfo startA2 = new ProcessStartInfo();
startA2.Arguments = string.Format("0 0 -skipintro -mod={0} -noSplash -noFilePatching -world=empty -connect={1} -port={2} \"-mod={3};expansion;\"", modsA2, ipA2, portA2, a2DirectoryPathModFriendly);
startA2.FileName = a2OADirectoryPath;
// Do you want to show a console window?
startA2.WindowStyle = ProcessWindowStyle.Hidden;
startA2.CreateNoWindow = false;
int exitCode;
// Run the external process & wait for it to finish
using (Process proc = Process.Start(startA2))
{
proc.WaitForExit();
// Retrieve the app's exit code
exitCode = proc.ExitCode;
}
}
所有的目录都在程序的后面声明为字符串。所以在他们选择文件后,他们有 4 个选项,其中一个使用 arma 2,如图所示,需要访问 Arma 2 exe 才能启动,问题是,它在重置时一直在擦除,所以有没有办法存储位置以供以后使用?
感谢您的阅读- BorderlineHypeR
【问题讨论】:
-
我不太确定该怎么做,但您也许可以在用户的计算机上保存一个配置/文本文件并搜索它。
-
有很多方法可以做到这一点,您可以自己将设置写入文本文件,您可以通过序列化对象并在需要时加载它来写入xml文件。