C#获取目录名称

时间:2012-05-17 06:17:34

标签: c#

我在c:\program files(x86)\company\application\中有一个包含所有应用文件的文件夹。

如何获得C:\program files(x86)\company之类的路径?

我无法使用Application.Startuppath,因为它会返回c:\program files)x86)\company\application

感谢。

3 个答案:

答案 0 :(得分:3)

使用Directory.GetParent(string path)

在您的应用中包含System.IO命名空间

using System.IO;

并使用

string desired_path = Directory.GetParent(Application.StartupPath);
                       // will return c:\program files(x86)\company
        // because app path will have c:\program files(x86)\company\application\

答案 1 :(得分:0)

请参阅此MSDN内容关于System.IO.Path.GetDirectoryName(string)

所以,包括:

using System.IO;

现在你的代码:

string ApplicationPath = Application.StartupPath;
string YourPath = Path.GetDirectoryName(ApplicationPath);
// This will get "C:\program files(x86)\company" from "C:\program files(x86)\company\application"

答案 2 :(得分:0)

最好使用Environment.CurrentDirectory,它会对您有所帮助。

请参阅http://msdn.microsoft.com/en-us/library/system.environment.currentdirectory%28v=vs.71%29.aspx

了解更多详情。

相关问题