C# 如何获取应用程序的路径(没有app.exe)?

C# 如何获取应用程序的路径(没有app.exe)?,c#,windows-mobile,compact-framework,filepath,C#,Windows Mobile,Compact Framework,Filepath,我想获得我的应用程序的路径,如:“\\ProgramFiles\\myApp”,我尝试使用以下代码: string path = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase; 但它返回的路径末尾有“\\myapp.exe” 我还尝试: string path = System.IO.Directory.GetCurrentDirectory(); 但它抛出了一个“NotSupportedExce

我想获得我的应用程序的路径,如:“\\ProgramFiles\\myApp”,我尝试使用以下代码:


string path = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
但它返回的路径末尾有“\\myapp.exe”

我还尝试:


string path = System.IO.Directory.GetCurrentDirectory();
但它抛出了一个“NotSupportedException”


有没有办法在末尾不使用.exe来获取路径?

应用程序。StartupPath
应该可以为您这样做

path = System.IO.Path.GetDirectoryName( path );
更新:从您的编辑中,我看到您正在Compact Framework上运行。然后Application.StartupPath将无法工作。这是我当时通常使用的结构:

private static string GetApplicationPath()
{
    return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
}

您可以使用Path.GetDirectoryName(string)方法将原始路径字符串作为参数传递给此方法。你想解决什么问题?也许你真的需要工作目录之类的东西

使用FileInfo对象提取目录名怎么样

Path.GetFileNameWithoutExtension(path);
在Vb.Net中:

fi = New FileInfo(System.Reflection.Assembly.GetExecutingAssembly.Location)
path = fi.DirectoryName

如果与您的案例一样是exe,请使用:

    // Summary:
    //     Gets the path for the executable file that started the 
    //     application, not including the executable name.    
    Application.StartupPath

比其他更简单:

using System.IO;
using System.Reflection;
...
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)

谢谢,但是这将返回文件名“myApp”,就像方法名所提到的一样。无论如何,谢谢你给我这个方法的指导。嘘!这就是我认为你想要的在C#app中找不到“Location”,但()中的所有内容都可以替换为我编写的文件路径,并且可以正常工作。谢谢。难道
getExecutiveGassembly
不需要
()
?e、 g.
getExecutionGassembly()
,还是仅仅是C#?谢谢你和danbyStrom,你的指南很好用。我正在尝试使用我的应用程序目录中的wav文件播放声音,所以我需要它的目录(没有app.exe)。嗯……这很简单,但找不到“位置”(至少在Windows Mobile应用程序中)。