我正在为管理员的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;
}
}
所有目录都在程序中作为strings进一步声明。因此,在选择文件后,它们有4个选项,其中一个使用arma 2,如此处所示,需要访问Arma 2 exe才能启动问题是,它一直在重置时擦除,那么有没有办法存储该位置供以后使用?
谢谢你的阅读 - BorderlineHypeR
答案 0 :(得分:0)
https://www.youtube.com/watch?v=P432z8q9iVE这是保存用户设置的最简单方法。
尽管如此,我更喜欢将对象序列化为XML或JSON(使用http://www.newtonsoft.com/json),我发现处理复杂设置(列表,词典等)时更容易,但我认为你不需要现在