繁体   English   中英

C#获取目录名称

[英]C# get directory name

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

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

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

谢谢。

使用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\

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

因此,包括:

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"

最好使用Environment.CurrentDirectory ,它将为您提供帮助。

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

更多细节。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM