首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

关于截去字符串的有关问题,多谢,

2011-12-20 
关于截去字符串的问题,谢谢,在线等!文件路径为:C:\我的文档\我的文件夹\cyh.txt我想截取字符串为:C:\我的

关于截去字符串的问题,谢谢,在线等!
文件路径为:C:\我的文档\我的文件夹\cyh.txt
我想截取字符串为:C:\我的文档\我的文件夹\
请问如何实现

注意:文件路径不确定,可以是任何长度的文件路径

谢谢,在线等!

[解决办法]
string str = @ "C:\我的文档\我的文件夹\cyh.txt ";
int Index = str.LastIndexOf(@ "\ ");
string NewStr = str.Substring(0, Index+1);
MessageBox.Show(NewStr);
[解决办法]
string fileName = @ "C:\我的文档\我的文件夹\cyh.txt ";
string path = System.IO.Path.GetDirectoryName(fileName);
MessageBox.Show(path);
[解决办法]
用.net自带的FileInfo类

System.IO.FileInfo fileInfo = new System.IO.FileInfo(@ "C:\我的文档\我的文件夹\cyh.txt ");
string folder = fileInfo.DirectoryName;
[解决办法]
string str1 = @ "C:\我的文档\我的文件夹\cyh.txt ";

string str2=str1.Substring(0,str1.Length-7);
[解决办法]
string str = @ "C:\我的文档\我的文件夹\cyh.txt ";
int Index = str.LastIndexOf(@ "\ ");
string NewStr = str.Substring(0, Index+1);
MessageBox.Show(NewStr);

热点排行