Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查找我的应用程序的位置';WPF中的可执行文件(C#或vb.net)?_C#_Wpf_Vb.net - Fatal编程技术网

查找我的应用程序的位置';WPF中的可执行文件(C#或vb.net)?

查找我的应用程序的位置';WPF中的可执行文件(C#或vb.net)?,c#,wpf,vb.net,C#,Wpf,Vb.net,如何在WPF(C#或VB.Net)中找到应用程序可执行文件的位置 我已将此代码用于windows窗体: Application.ExecutablePath.ToString(); 但使用WPF时,我从Visual Studio收到了以下错误: System.Window.Application不包含可执行路径的定义 System.Reflection.Assembly.getExecutionGassembly().Location应该可以工作。有几种选择: Directory.GetPar

如何在WPF(C#或VB.Net)中找到应用程序可执行文件的位置

我已将此代码用于windows窗体:

Application.ExecutablePath.ToString();
但使用WPF时,我从Visual Studio收到了以下错误:

System.Window.Application不包含可执行路径的定义


System.Reflection.Assembly.getExecutionGassembly().Location
应该可以工作。

有几种选择:

Directory.GetParent(Assembly.GetExecutingAssembly().Location)

System.AppDomain.CurrentDomain.BaseDirectory
仅在VB中:

My.Application.Info.DirectoryPath

如果代码位于库中,则执行程序集可以是DLL:

var executingAssembly = Assembly.GetExecutingAssembly(); //MyLibrary.dll
var callingAssembly = Assembly.GetCallingAssembly(); //MyLibrary.dll
var entryAssembly = Assembly.GetEntryAssembly(); //WpfApp.exe or MyLibrary.dll
所以我找到的最好的方法是:

因此,在您的情况下,您可以找到部件的位置:

var location = wpfAssembly.Location;

根据其他人的回答,下面的示例演示如何从路径中删除可执行文件名,并将结果与某些子文件夹和文件名相结合:

在Hotspotizer()的更新版本中,我刚刚添加了对启动时加载手势集合文件的支持(如果在Library\Default.hsjson中找到),方法是使用以下代码:

const string GESTURE_COLLECTION_LIBRARY_PATH = "Library"
const string DEFAULT_GESTURE_COLLECTION = "Default.hsjson"

//...

LoadGestureCollection(
  Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
  GESTURE_COLLECTION_LIBRARY_PATH,
  DEFAULT_GESTURE_COLLECTION));
这对您很有用: Application.ExecutablePath等于:

Process.GetCurrentProcess().MainModule.FileName;

这是我用的。它甚至可以在调试器中工作

using System.IO;
using System.Diagnostics;

public static string GetMyBinDirectory()
{
    return Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
}

它使用了一些功能强大的类,如和环境。CurrentDirectory返回exe文件的父目录。此文件的可能副本可能无法始终正常工作。它将获取正在执行的程序集,而不是应用程序的可执行文件。这意味着,如果希望应用程序的可执行文件通过引用的DLL运行,这将不起作用。即使在调试器中运行,它也会访问常规的.exe程序集。XXX.vshost.exe.System.AppDomain.CurrentDomain.BaseDirectory都不会返回当前目录,而不是可执行位置,即从outlook运行时(作为\\server\folder\file.exe的链接发送,它会将BaseDirectory设置为用户文档而不是可执行位置这是唯一适合我的方法。替代方法-GetExecutionGassembly为我提供了dll而不是exe(如上所述),并且从我编码的位置无法使用Application.ExecutablePath。
using System.IO;
using System.Diagnostics;

public static string GetMyBinDirectory()
{
    return Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
}