什麼是最好的C#(CSHARP)當量以下VB(VB.NET,VisualBasic中)聲明:轉換VB到C# - My.Application.Info.DirectoryPath
My.Application.Info.DirectoryPath
My.Computer.Clipboard
My.Computer.Audio.PlaySystemSound()
My.Application.Shutdown()
什麼是最好的C#(CSHARP)當量以下VB(VB.NET,VisualBasic中)聲明:轉換VB到C# - My.Application.Info.DirectoryPath
My.Application.Info.DirectoryPath
My.Computer.Clipboard
My.Computer.Audio.PlaySystemSound()
My.Application.Shutdown()
Application.ExecutablePath
System.Windows.Forms.Clipboard
System.Media。*
Application.Exit
這可能不是什麼您正在尋找,但爲了防止您想要使用快捷方式,如果添加對Microsoft.VisualBasic程序集的引用,則可以使用VB程序員通過命名空間MyServices訪問的漂亮工具。
好捷徑,但是,我一直在尋找普遍的選擇。我相信這會幫助別人。 – 2009-08-11 13:12:53
如果要轉換一個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();
我覺得你海RCH就是這句話:
Application.StartupPath;
//Get file path without file name.
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)
這些My.Application.Info.DirectoryPath AppDomain.CurrentDomain.BaseDirectory爲我提供了不同的答案。正確的答案似乎是GetFilePath(System.Reflection.Assembly.GetExecutingAssembly.Location) – user2728841 2017-12-27 14:52:25
從反編譯Microsoft.VisualBasic.dll中,調用My.Application.Info.DirectoryPath
當被執行的實際代碼是:
Path.GetDirectoryName(
new AssemblyInfo(
Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly()).Location);
System.IO.Directory.GetParent(Application.ExecutablePath)
是完全一樣的:
My.Application.Info.DirectoryPath
如果你只做:
Application.ExecutablePath
你會得到執行文件追加到路徑,這可能根本沒有用。
根據[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
這些對於Windows Forms應用程序來說可能是正確的 – 2009-08-11 13:20:41