dev*_*xer 15 .net c# wpf file-io windows-xp
背景:
我为我的应用程序构建了一个安装程序,所有我的dll和内容文件都被正确地复制到了C:\Program Files\MyCompany\MyApp
目录中.当我从Visual Studio运行我的应用程序时,一切都很好.但是,当我运行我的应用程序的安装版本时,我得到了一个DirectoryNotFoundException
.问题似乎与Environment.CurrentDirectory
.
我原以为Environment.CurrentDirectory
是......
"C:\\Program Files\\MyCompany\\MyApp"
Run Code Online (Sandbox Code Playgroud)
......但实际上......
"C:\\Documents and Settings\\DanThMan"
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?我该如何解决这个问题?
谢谢.
编辑:
好的,嗯.如果我运行"开始"菜单快捷方式,则只会出现此问题 如果我直接运行MyApp.exe,一切都很好.
编辑2:
我想我现在已经深究了这一点.在我的安装程序(Visual Studio SetupProject)中,"开始菜单"快捷方式具有一个名为WorkingFolder
"安装快捷方式目标应用程序的文件夹" 的属性.我不小心设置WorkingFolder
了"MyCompany".它应该是"应用程序文件夹".现在我已正确设置,Environment.CurrentDirectory
再次按预期工作.感谢你的帮助.
编辑3:
但是,阅读下面的警告,我决定使用以下内容作为替代Environment.CurrentDirectory
:
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
Run Code Online (Sandbox Code Playgroud)
Ale*_*vis 35
如果要获取运行可执行文件的目录的路径,则不应依赖于Environment.CurrentDirectory,因为它可以通过多种方式进行更改(shotrtcut settings等).请尝试以下选项之一:
System.IO.Path.GetDirectoryName(Application.ExecutablePath);
Run Code Online (Sandbox Code Playgroud)
要么
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
Run Code Online (Sandbox Code Playgroud)
用Application.StartupPath
而不是Environment.CurrentDirectory
.
我遇到了类似的问题,在这种情况CurrentDirectory
下,无意识地改变了OpenFileDialog
这种问题.
在您的情况下,听起来您正在启动申请表的过程正在改变CurrentDirectory
您所不知道的.
归档时间: |
|
查看次数: |
12595 次 |
最近记录: |