将VB转换为C#-My.Application.Info.DirectoryPath

将VB转换为C#-My.Application.Info.DirectoryPath,c#,vb.net,C#,Vb.net,以下VB(VB.NET,VisualBasic)语句的最佳C#(csharp)等价物是什么: My.Application.Info.DirectoryPath My.Computer.Clipboard My.Computer.Audio.PlaySystemSound() My.Application.Shutdown() 这可能不是您想要的,但如果您想走捷径,如果您添加了对Microsoft.VisualBasic程序集的引用,您可以使用VB程序员通过MyServices命名空间访

以下VB(VB.NET,VisualBasic)语句的最佳C#(csharp)等价物是什么:

My.Application.Info.DirectoryPath

My.Computer.Clipboard

My.Computer.Audio.PlaySystemSound()

My.Application.Shutdown()

这可能不是您想要的,但如果您想走捷径,如果您添加了对Microsoft.VisualBasic程序集的引用,您可以使用VB程序员通过MyServices命名空间访问的漂亮工具。

Application.ExecutablePath

System.Windows.Forms.Clipboard

System.Media*


应用程序。退出

如果要转换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();

我想你搜索的是这句话:

Application.StartupPath;
//Get file path without file name.

通过反编译Microsoft.VisualBasic.dll,调用
My.Application.Info.DirectoryPath
时执行的实际代码为:

Path.GetDirectoryName(
    new AssemblyInfo(
        Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly()).Location);
与以下内容完全相同:

My.Application.Info.DirectoryPath
如果你只做:

Application.ExecutablePath
您将获得附加到路径的执行文件,这可能根本没有用。

以下

using System.IO;
Directory.GetCurrentDirectory()

很好的捷径,但是是的,我在寻找通用的替代品。但我相信这会对某些人有所帮助。据我所知,这是不正确的。GetParent方法返回DirectoryInfo对象,而不是路径(字符串)。您需要使用FullName属性来实际获取路径。不要使用
System.IO.Directory.GetParent(Application.ExecutablePath)
,而是使用
System.IO.Directory.GetParent(Application.ExecutablePath.FullName
。这些My.Application.Info.DirectoryPath AppDomain.CurrentDomain.BaseDirectory为我提供了不同的答案。正确的答案似乎是GetFilePath(System.Reflection.Assembly.GetExecutionGassembly.Location),感谢@user2728841,它的工作正常。GetFilePath(System.Reflection.Assembly.GetExecutionGassembly.Location)。这有点不正确。GetExecutionGassembly.Location。GetExecutionGassembly()方法中缺少大括号()。请解释您的答案,以及它如何优于其他答案。和My.Application.Info.AssemblyName?
Application.ExecutablePath
using System.IO;
Directory.GetCurrentDirectory()