当运行安装程序Environment.CurrentDirectory正在产生意想不到的效果 [英] Environment.CurrentDirectory is yielding unexpected results when running installed app

查看:350
本文介绍了当运行安装程序Environment.CurrentDirectory正在产生意想不到的效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:

我建立了一个安装程序我的应用程序,和我所有的dll的文件和内容文件都得到正确地复制到 C:\ Program Files文件\ MyCompany的\ MyApp的目录。当我从Visual Studio中运行我的应用程序,一切都很正常。当我运行安装我的应用程序的版本,但是,我得到一个 DirectoryNotFoundException 。这个问题似乎与 Environment.CurrentDirectory

我期待 Environment.CurrentDirectory 是...

 C:\\ Program Files文件\\ \\ MyCompany的MyApp的
 

...但它实际上是...

 C:\\的Documents and Settings \\ DanThMan
 

这是怎么回事呢?我该如何解决这个问题?

感谢。

编辑:

好吧,嗯。只有当我运行开始菜单快捷方式发生此问题。如果我MyApp.exe的直接运行,一切都很好。

编辑2:

我想我已经得到了这条底线了。在我的安装程序(这是一个Visual Studio SetupProject),开始菜单快捷方式有一个叫做财产 WorkingFolder ,其中指定快捷方式的目标应用程序将被安装的文件夹。我不小心设置 WorkingFolder 来MyCompany的。它应该是应用程序文件夹。现在,我有它设置正确, Environment.CurrentDirectory 再次按预期工作。感谢您的帮助。

修改3:

不过,看完下面的警告,我已经决定去与以下,以替换 Environment.CurrentDirectory

  System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly()位置。);
 

解决方案

如果你想获得的路径目录下的可执行文件运行时,你不应该依赖于Environment.CurrentDirectory,因为它可以在改变的方式编号(shotrtcut设置等)。请尝试以下选项之一,而不是:

  System.IO.Path.GetDirectoryName(Application.ExecutablePath);
 

  System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly()位置。);
 

Background:

I built an installer for my app, and all my dll's and content files are getting correctly copied to the C:\Program Files\MyCompany\MyApp directory. When I run my app from Visual Studio, everything works great. When I run the installed version of my app, however, I get a DirectoryNotFoundException. The problem seems to be with Environment.CurrentDirectory.

I was expecting Environment.CurrentDirectory to be...

"C:\\Program Files\\MyCompany\\MyApp"

...but it was actually...

"C:\\Documents and Settings\\DanThMan"

What's going on here? How do I solve this?

Thanks.

EDIT:

Okay, hmm. This problem only occurs if I run the Start Menu shortcut. If I run MyApp.exe directly, everything is fine.

EDIT 2:

I think I've gotten to the bottom of this now. In my Installer (which is a Visual Studio SetupProject), the Start Menu shortcut has a property called WorkingFolder, which "Specifies the folder where the target application for the shortcut will be installed." I had accidentally set WorkingFolder to "MyCompany". It should be "Application Folder". Now that I have it set correctly, Environment.CurrentDirectory is once again working as expected. Thanks for all your help.

EDIT 3:

However, reading the warnings below, I have decided to go with the following as a replacement for Environment.CurrentDirectory:

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

解决方案

If you want to get the path to the directory under which your executable runs, you should not rely on the Environment.CurrentDirectory, since it can be changed in a number of ways (shotrtcut settings, etc). Try one of these options instead:

System.IO.Path.GetDirectoryName(Application.ExecutablePath);

or

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);

这篇关于当运行安装程序Environment.CurrentDirectory正在产生意想不到的效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆