【问题标题】:I need my program to save the location that the user has selected the executable from我需要我的程序来保存用户从中选择可执行文件的位置
【发布时间】: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文件。

标签: c# xml input save


【解决方案1】:

https://www.youtube.com/watch?v=P432z8q9iVE这是迄今为止保存用户设置的最简单方法。

不过,我更喜欢将对象序列化为 XML 或 JSON(使用 http://www.newtonsoft.com/json),我发现在处理复杂设置(列表、字典等)时更容易,但我认为您现在不需要它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多