如何从 C# 中的完整路径中获取一部分?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/912443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 02:39:36  来源:igfitidea点击:

How to get a part from the full path in C#?

c#stringdirectory

提问by crazy_itgal

I have a full path as given below.

我有一个完整的路径,如下所示。

C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd

How can the DTDs "part" to be fetched from this whole part?

如何从整个部分中获取 DTD 的“部分”?

Desired output:

期望的输出:

C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug??\DannyGoXuk.DTDs

Can I use String's methods for this?
If yes, then how to fetch it?

我可以使用String's 的方法吗?
如果是,那么如何获取它?

采纳答案by JimDel

Edit: Please read the OP's question and all of her comments carefully before downvotiong this. The OP's title question isn't EXACTLY what she wanted. My answer gave her what she needed to solve her problem. Which is why she voted it the answer. Yes, Joel's answer is correct if specifically answering the title question. But after reading her comments, you'll see that not exactly what she was looking for. Thanks.

编辑:在downvotiong之前,请仔细阅读OP的问题和她的所有评论。OP 的标题问题并不完全是她想要的。我的回答为她提供了解决问题所需的一切。这就是为什么她投票给它的答案。是的,如果专门回答标题问题,乔尔的回答是正确的。但在阅读她的评论后,你会发现这并不完全是她想要的。谢谢。

Use this ...

用这个 ...

string strFullPath = @"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd";
string strDirName; 
int intLocation, intLength;

intLength = strFullPath.Length;
intLocation = strFullPath.IndexOf("DTDs");

strDirName = strFullPath.Substring(0, intLocation); 

textBox2.Text = strDirName;

回答by Joel Coehoorn

Use System.IO.Path.GetDirectoryName()for the entire path, or new DirectoryInfo(path).Parent.Namefor just the name of that one folder.

使用System.IO.Path.GetDirectoryName()整个路径,或new DirectoryInfo(path).Parent.Name为一个文件夹的只是名字。



There is no directory named "DTDs" in the path you posted.IT looks like there's a filenamed "DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd", but the periods (.) in that path are not valid directory separator characters. Did you mean "DannyGoXuk\DTDs\xhtml-math-svg-flat.dtd"?

您发布的路径中没有名为“DTD”的目录。看起来有一个名为的文件"DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd",但该路径中的句点 (.) 不是有效的目录分隔符。你的意思是"DannyGoXuk\DTDs\xhtml-math-svg-flat.dtd"

If that's the case, given that entire new path, you want something like this to return a list of files in the DTDsfolder:

如果是这种情况,考虑到整个新路径,您希望像这样返回文件DTDs夹中的文件列表:

string path = @"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk\DTDs\xhtml-math-svg-flat.dtd";
string[] files = new DirectoryInfo(path).Parent.GetFiles();


in properties window i choose Build Type as Embedded resource.

在属性窗口中,我选择构建类型作为嵌入式资源。

And now we finally get to it. When you choose "Embedded Resource", the item is bundled into your executable program file. There is no direct path anymore. Instead, set your Build Type to "Content" and set "Copy to Output Directory" to "Copy Always" or "Copy if Newer".

现在我们终于明白了。当您选择“嵌入式资源”时,该项目将被捆绑到您的可执行程序文件中。 没有直接的路径了。相反,将您的构建类型设置为“内容”并将“复制到输出目录”设置为“始终复制”或“如果较新则复制”。

回答by Mehrdad Afshari

Calling

打电话

System.IO.Path.GetFileName

with the full directory path returns the last part of the path which is a directory name. GetDirectoryNamereturns the whole path of parent directory which is unwanted.

使用完整目录路径返回路径的最后一部分,即目录名称。GetDirectoryName返回不需要的父目录的整个路径。

If you have a file name and you just want the name of the parent directory:

如果您有一个文件名,而您只想要父目录的名称:

var directoryFullPath = Path.GetDirectoryName(@"C:\DTDs\mydtd.dtd");  // C:\DTDs
var directoryName = Path.GetFileName(directoryFullPath);  // DTDs

回答by harpo

System.IO.Path.GetFileName( System.IO.Path.GetDirectoryName( fullPath ) )

That will return just the name of the folder containing the file.

这将仅返回包含该文件的文件夹的名称。

For

为了

C:\windows\system32\user32.dll

this will return

这将返回

system32

I'm inferring that that's what you want.

我推断这就是你想要的。

回答by Beatles1692

You can use:

您可以使用:

System.IO.Path.GetDirectoryName(path);

回答by JP Alioto

You can use Path...

您可以使用路径...

Path.GetDirectoryName(myStr);

回答by user10635

Don't use string manipulation directly. Instead use GetDirectoryNameof the Path class:

不要直接使用字符串操作。而是使用GetDirectoryNamePath 类:

System.IO.Path.GetDirectoryName(myPath);

回答by David McEwing

Use the FileInfo object...

使用 FileInfo 对象...

FileInfo info = new FileInfo(@"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd");
string directoryName = info.Directory.FullName;

The file doesn't even have to really exist.

该文件甚至不必真的存在。

回答by RichardOD

Path.GetDirectoryon the path you have specified returns:

Path.GetDirectory在您指定的路径上返回:

"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug"

"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug"

Try it yourself:

自己试试:

var path = Path.GetDirectoryName(@"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd");

Your question is a little strange though- there is no directory called DTDs.

不过,您的问题有点奇怪——没有名为 DTD 的目录。

回答by James

Use:

用:

string dirName = new DirectoryInfo(fullPath).name;