如何确定 WPF(xbap) 应用程序的启动文件夹?
我已经对此进行了几个小时的调查。我发现了许多链接,包括 SO 上的几个链接,它们声称显示如何找到启动路径或应用程序目录。建议的所有解决方案均返回位置:
C:\Users\<我的用户名>\AppData\Local\Apps\2.0\XO8PWL8B.5HH\1GZX7M0H.N1J\<临时位置>\
当我的 WPF xbap 从远程位置运行时。我需要确定远程位置的实际文件夹。
我将其部署到内部服务器 ABCDEF,因此为了运行此应用程序,我输入:
\\ABCDEF\myApp.xbap
我想以编程方式确定服务器和文件夹。我这样做的原因是,每次发布 WPF 时,都会打开“每次发布时自动递增修订版本
”。附加 DLL 所在的文件夹发生变化,该程序依赖的附加程序。
我希望能够动态确定要查看的正确文件夹。
我已经尝试过:
'Dim path As String = Reflection.Assembly.GetEntryAssembly().Location
'Dim Path As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)
'System.IO.Path.GetDirectoryName(System.Reflection. Assembly.GetExecutingAssembly.Location)
'application.StartupPath
所有这些都不起作用。
这些链接建议了我尝试过的许多方法:
I have been investigating this for several hours. I have found numerous links, including several here on SO which claim to show how to find the startup path, or the application directory. All of the solutions suggested return a location:
C:\Users\<my user name>\AppData\Local\Apps\2.0\XO8PWL8B.5HH\1GZX7M0H.N1J\<temp location>\
When my WPF xbap is run from a remote location. I need to determine the actual folder of the remote location.
I am deploying this to an internal server ABCDEF, so in order to run this application I am entering:
\\ABCDEF\myApp.xbap
I want to programmatically determine the server and folder. My reason for this is that each time you publish a WPF with "automatically increment revision with each publish
" turned on. The folder where additional DLL's are located changes, additional programs that this program depends on.
I want to be able to dynamically determine the correct folder to look at.
I have tried:
'Dim path As String = Reflection.Assembly.GetEntryAssembly().Location
'Dim Path As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)
'System.IO.Path.GetDirectoryName(System.Reflection. Assembly.GetExecutingAssembly.Location)
'application.StartupPath
All of which did not work.
These links suggested many of the methods I have tried:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这会引导你走向正确的方向。由于听起来您是通过 ClickOnce 进行部署,因此您应该在此处获取所需的信息:
此类可以在 System.Deployment.dll 中找到,
不幸的是,CurrentDeployment 对象没有实际上并没有告诉您应用程序所在的位置,因此您必须做更多工作:(
如果您这样称呼:
您将得到类似这样的内容(对于数据目录)
C:\Users\Administrator\AppData\Local\Apps\2.0 \Data\BVPTZA5G.3AC\WC2WBZ92.D96\ expe..tion_ba14dd6bedbd6876_0000.0009_7919ca38a4e51341\Data\1.0.0.0
从那里您需要获取上面粗体文件夹的文件夹名称,因为
应用程序位于此处(不是数据目录):
C:\Users\Administrator\AppData\Local\Apps\2.0\RCVHD71Y.7CQ\BC42YMHT.ZQ0\ expe..tion_ba14dd6bedbd6876_0000.0009_7919ca38a4e51341
所以我会创建一个函数,该函数
我认为在 ApplicationDeployment 类型上创建一个扩展方法是合适的。
希望有帮助
I think this will steer you in the right direction. Since it sounds like you're deploying via ClickOnce you should get the information you need here:
this class can be found in the
System.Deployment.dll
Unfortunately the
CurrentDeployment
object doesn't actually tell you where the application lives so you have to do some more work :(if you call this:
you'll get something like this(for the datadirectory)
C:\Users\Administrator\AppData\Local\Apps\2.0\Data\BVPTZA5G.3AC\WC2WBZ92.D96\ expe..tion_ba14dd6bedbd6876_0000.0009_7919ca38a4e51341\Data\1.0.0.0
from there you need to get the folder name of the bold folder above because
the application lives here(not the data directory):
C:\Users\Administrator\AppData\Local\Apps\2.0\RCVHD71Y.7CQ\BC42YMHT.ZQ0\ expe..tion_ba14dd6bedbd6876_0000.0009_7919ca38a4e51341
So I would create a function that
I think creating an extension method on type ApplicationDeployment would be fitting.
Hope that helps
这对我有用:
string exePath = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName;
字符串 dir = Path.GetDirectoryName(exePath);
This worked for me:
string exePath = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName;
string dir = Path.GetDirectoryName(exePath);