如何确定 WPF(xbap) 应用程序的启动文件夹?

发布于 2024-11-07 18:25:32 字数 1528 浏览 0 评论 0原文

我已经对此进行了几个小时的调查。我发现了许多链接,包括 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

所有这些都不起作用。

这些链接建议了我尝试过的许多方法:

链接1

链接2

链接3

Link4

Link5

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:

Link1

Link2

Link3

Link4

Link5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恍梦境° 2024-11-14 18:25:32

我认为这会引导你走向正确的方向。由于听起来您是通过 ClickOnce 进行部署,因此您应该在此处获取所需的信息:

System.Deployment.Application.ApplicationDeployment.CurrentDeployment

此类可以在 System.Deployment.dll 中找到,

不幸的是,CurrentDeployment 对象没有实际上并没有告诉您应用程序所在的位置,因此您必须做更多工作:(

如果您这样称呼:

var datadir = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.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

从那里您需要获取上面粗体文件夹的文件夹名称,因为
应用程序位于此处(不是数据目录):

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:

System.Deployment.Application.ApplicationDeployment.CurrentDeployment

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:

var datadir = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.DataDirectory

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

  • Takes in the first filepath
  • Determines the randomly generated foldername
  • finds the folder of the application based on that folder name(which contains your executables)

I think creating an extension method on type ApplicationDeployment would be fitting.

Hope that helps

聆听风音 2024-11-14 18:25:32

这对我有用:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文