2009-07-23 115 views

回答

21

Application.ExecutablePath

System.Windows.Forms.Clipboard

System.Media。*

Application.Exit

+2

這些對於Windows Forms應用程序來說可能是正確的 – 2009-08-11 13:20:41

2

這可能不是什麼您正在尋找,但爲了防止您想要使用快捷方式,如果添加對Microsoft.VisualBasic程序集的引用,則可以使用VB程序員通過命名空間MyServices訪問的漂亮工具。

+0

好捷徑,但是,我一直在尋找普遍的選擇。我相信這會幫助別人。 – 2009-08-11 13:12:53

3

如果要轉換一個WPF應用程序,你可以使用以下:

System.Reflection.Assembly.GetExecutingAssembly().Location; 
//gets file path with file name 

System.Windows.Clipboard; 

System.Media.SystemSounds.[Sound].Play(); 

System.Windows.Application.Current.Shutdown(); 
0

我覺得你海RCH就是這句話:

Application.StartupPath; 
//Get file path without file name. 
3
My.Application.Info.DirectoryPath 
    AppDomain.CurrentDomain.BaseDirectory 

My.Computer.Clipboard 
    System.Windows.Clipboard //(WPF) 
    System.Windows.Forms.Clipboard //(WinForms) 

My.Computer.Audio.PlaySystemSound() 
    System.Media.SystemSounds.*.Play() 

My.Application.Shutdown() 
    System.Windows.Forms.Application.Exit() //(WinForms) 
    or 
    System.Windows.Application.Current.Shutdown() //(WPF) 
    or 
    System.Environment.Exit(ExitCode) //(Both WinForms & WPF) 
+0

這些My.Application.Info.DirectoryPath AppDomain.CurrentDomain.BaseDirectory爲我提供了不同的答案。正確的答案似乎是GetFilePath(System.Reflection.Assembly.GetExecutingAssembly.Location) – user2728841 2017-12-27 14:52:25

2

從反編譯Microsoft.VisualBasic.dll中,調用My.Application.Info.DirectoryPath當被執行的實際代碼是:

Path.GetDirectoryName(
    new AssemblyInfo(
     Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly()).Location); 
1
System.IO.Directory.GetParent(Application.ExecutablePath) 

是完全一樣的:

My.Application.Info.DirectoryPath 

如果你只做:

Application.ExecutablePath 

你會得到執行文件追加到路徑,這可能根本沒有用。

+0

根據[Scuzzlebutt](http://stackoverflow.com/a/36102900/1677912),這是不正確的。 GetParent方法返回一個DirectoryInfo對象,而不是路徑(字符串)。您需要使用FullName屬性來實際獲取路徑。而不是`System.IO.Directory.GetParent(Application.ExecutablePath)`,使用`System.IO.Directory.GetParent(Application.ExecutablePath).FullName`。 – Mogsdad 2016-04-07 21:11:52