如何在C#.net Windows应用程序中拆分路径 [英] how to split path in C# .net windows application
本文介绍了如何在C#.net Windows应用程序中拆分路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如..:如果我有D:\ backup \ Suresh \ Project \ Welcome \ MyFile \ show.jpg
从此路径中,我只需要D:\ backup \ Suresh \ Project \
for eg..: if i have D:\backup\Suresh\Project\Welcome\MyFile\show.jpg
from this path I need only D:\backup\Suresh\Project\
推荐答案
否,不要拆分任何内容,也不要使用直接的字符串操作其他建议.
取而代之的是,首先使用System.IO.Path.GetDirectoryName
提取目录.参见:
http://msdn.microsoft.com/en-us/library/system. io.path.getdirectoryname.aspx [ ^ ].
关于结果,请使用方法System.IO.Directory.GetParent
所需次数,请参阅:
http://msdn.microsoft.com/en-us/library/system. io.directory.getparent.aspx [ ^ ].
与直接字符串操作(您很容易出错)不同,这些方法可靠且与平台无关.
—SA
No, don''t split anything and don''t use direct string manipulations as other advised.
Instead, first extract the directory usingSystem.IO.Path.GetDirectoryName
; see:
http://msdn.microsoft.com/en-us/library/system.io.path.getdirectoryname.aspx[^].
On the result, use the methodSystem.IO.Directory.GetParent
required number of times, see:
http://msdn.microsoft.com/en-us/library/system.io.directory.getparent.aspx[^].
Unlike direct string manipulations where you can easily make a mistake, these methods are reliable and platform independent.
—SA
这篇关于如何在C#.net Windows应用程序中拆分路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文