如何在WPF(C#或VB.Net)中找到应用程序可执行文件的位置?
我在windows窗体中使用了这段代码:
Application.ExecutablePath.ToString();
但是使用WPF我从Visual Studio收到了这个错误:
System.Window.Application不包含ExecutablePath的定义。
答案 0 :(得分:85)
System.Reflection.Assembly.GetExecutingAssembly().Location
应该有用。
答案 1 :(得分:31)
有几种选择:
Directory.GetParent(Assembly.GetExecutingAssembly().Location)
System.AppDomain.CurrentDomain.BaseDirectory
仅限VB:
My.Application.Info.DirectoryPath
答案 2 :(得分:6)
这对你有用: Application.ExecutablePath等于:
Process.GetCurrentProcess().MainModule.FileName;
答案 3 :(得分:2)
如果代码位于库中,则执行程序集可以是DLL:
var executingAssembly = Assembly.GetExecutingAssembly(); //MyLibrary.dll
var callingAssembly = Assembly.GetCallingAssembly(); //MyLibrary.dll
var entryAssembly = Assembly.GetEntryAssembly(); //WpfApp.exe or MyLibrary.dll
所以我找到的最好的方法是(C#):
var wpfAssembly = (AppDomain.CurrentDomain
.GetAssemblies()
.Where(item => item.EntryPoint != null)
.Select(item =>
new {item, applicationType = item.GetType(item.GetName().Name + ".App", false)})
.Where(a => a.applicationType != null && typeof(System.Windows.Application)
.IsAssignableFrom(a.applicationType))
.Select(a => a.item))
.FirstOrDefault();
因此,在您的情况下,您可以找到程序集的位置:
var location = wpfAssembly.Location;
答案 4 :(得分:1)
这就是我用的。即使在调试器中也可以使用。
css = 'body > main > section > section > div.ut-navigation-container-view--content > div > div.ut-pinned-list-container.ut-content-container > div > div.ut-pinned-list > div.ut-item-search-view > div:nth-child(2)'
iElement = browser.find_element_by_css_selector(css)
SpanVariableValue = iElement.text
#prints selected value or default
print (SpanVariableValue )
iElement.click()
SpanVariableValue = iElement.text
#prints all elemets of drop-down
print (SpanVariableValue)
#drop-down doesn't work after the following code
browser.execute_script("arguments[0].innerText = 'SILVER'", iElement)
它使用了一些强大的类,例如Process和ProcessModule
答案 5 :(得分:0)
基于其他答案,这里有一个示例,说明如何从路径中删除可执行文件名,并将结果与某个子文件夹和文件名组合:
在我更新的Hotspotizer版本(http://github.com/birbilis/Hotspotizer)中,我刚刚添加了对启动时加载Gesture Collection文件的支持,如果在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));
答案 6 :(得分:0)
Environment.CurrentDirectory
返回exe文件的父目录