当运行安装程序Environment.CurrentDirectory正在产生意想不到的效果安装程序、意想不到、效果、CurrentDirectory

由网友(执画浅相思)分享简介:背景: 我建立了一个安装程序我的应用程序,和我所有的dll的文件和内容文件都得到正确地复制到 C:\ Program Files文件\ MyCompany的\ MyApp的目录。当我从Visual Studio中运行我的应用程序,一切都很正常。当我运行安装我的应用程序的版本,但是,我得到一个 DirectoryNot...

背景:

我建立了一个安装程序我的应用程序,和我所有的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 再次按预期工作。感谢您的帮助。

运行Directplay语言安装程序时出错

修改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 FilesMyCompanyMyApp 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 FilesMyCompanyMyApp"

...but it was actually...

"C:Documents and SettingsDanThMan"

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);

阅读全文

相关推荐

最新文章