StartProcess 使用 Properties.Settings 中的值导致异常结果
当尝试使用 Properties.Settings 中存储的应用程序名称启动应用程序时,我看到奇怪的行为。如果我在使用前没有重新设置该值(设置为相同值),则启动的应用程序将无法获取其应用程序设置的正确位置。也许显示代码会澄清我所说的内容。
这是启动新进程的代码。非常简单的东西。
private void StartNewApplication()
{
Process mainAppProcess = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = Properties.Settings.Default.TheApplicationPath;
startInfo.WindowStyle = ProcessWindowStyle.Normal;
mainAppProcess.StartInfo = startInfo;
mainAppProcess.Start();
mainAppProcess.WaitForExit();
}
我有另一个函数,它可以通过在标准 OpenFileDialog 中浏览文件来简单地设置设置。我不会费心在这里展示这一点,除了代码片段:
if (fileDialog.ShowDialog().Value == true)
{
Properties.Settings.Default.TheApplicationPath = fileDialog.FileName;
Properties.Settings.Default.Save();
}
失败的代码(我无法控制)类似于:
private static string GetConfigFolder()
{
string configFolder = ConfigurationManager.AppSettings.Get("ConfigFolder");
configFolder = Path.GetFullPath(configFolder);
return string.IsNullOrEmpty(configFolder) ? Environment.CurrentDirectory : configFolder;
}
由于 AppSettings 值总是返回“.”,因此 Path.GetFullPath 调用返回当前目录。如果我不重新设置Properties.Setting值,它就是启动应用程序的程序的路径;如果我重新设置设置,它就是已启动的应用程序的路径。
有什么想法吗?
谢谢, 重量
I am seeing odd behavior when trying to launch an application using an application name stored in Properties.Settings. If I don't re-set the value (to the same value) before using it, the launched application will fail to get the correct location for its application settings. Perhaps showing the code will clear up what I'm saying.
Here is the code that starts the new process. Pretty straight-forward stuff.
private void StartNewApplication()
{
Process mainAppProcess = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = Properties.Settings.Default.TheApplicationPath;
startInfo.WindowStyle = ProcessWindowStyle.Normal;
mainAppProcess.StartInfo = startInfo;
mainAppProcess.Start();
mainAppProcess.WaitForExit();
}
I have another function that simply sets the Setting by browsing for the file in a standard OpenFileDialog. I won't bother showing that here, except for the snippet:
if (fileDialog.ShowDialog().Value == true)
{
Properties.Settings.Default.TheApplicationPath = fileDialog.FileName;
Properties.Settings.Default.Save();
}
The code that was failing (which I have no control over) is something like:
private static string GetConfigFolder()
{
string configFolder = ConfigurationManager.AppSettings.Get("ConfigFolder");
configFolder = Path.GetFullPath(configFolder);
return string.IsNullOrEmpty(configFolder) ? Environment.CurrentDirectory : configFolder;
}
Since the AppSettings value is always coming back ".", the Path.GetFullPath call returns the current directory. If I don't re-set the Properties.Setting value, it is the path of the program that starts the application; if I re-set the Setting, it is the path of the application that has been launched.
Any ideas?
Thanks,
WtS
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置保存在安装的上下文中。如果您正在调试或以其他方式在 Visual Studio 之外运行此设置,则每次都会使用默认值,并且当您保存设置时,它只会在调试会话期间保留。
换句话说,查看
app.config
文件中的该设置。调试并更改值。再次查看app.config
文件。它不会更新。另一方面,如果您部署此应用程序,app.config
将被更新(但请注意,如果您重新部署或重新安装,默认情况下,保存的设置将再次被覆盖)。The settings are saved within the context of an installation. If you are debugging or otherwise running this out of Visual Studio, the default value is going to be used every time, and when you save the setting, it is only going to stick for the duration of your debugging session.
Put another way, look at that setting in your
app.config
file. Debug and change the value. Look again at theapp.config
file. It does not get updated. If you deploy this application, on the other hand,app.config
would be updated (note, however, that it you redeploy or reinstall, the saved settings will again be overwritten, by default).