文件路径为:C:\我的文档\我的文件夹\cyh.txt
我想截取字符串为:C:\我的文档\我的文件夹\
请问如何实现注意:文件路径不确定,可以是任何长度的文件路径谢谢,在线等!

解决方案 »

  1.   

    string str = @"C:\我的文档\我的文件夹\cyh.txt";
                int Index = str.LastIndexOf(@"\");
                string NewStr = str.Substring(0, Index+1);
                MessageBox.Show(NewStr);
      

  2.   

    string fileName = @"C:\我的文档\我的文件夹\cyh.txt";
    string path = System.IO.Path.GetDirectoryName(fileName);
    MessageBox.Show(path);
      

  3.   

    用.net自带的FileInfo类System.IO.FileInfo fileInfo = new System.IO.FileInfo(@"C:\我的文档\我的文件夹\cyh.txt");
    string folder = fileInfo.DirectoryName;
      

  4.   


    何必要自己分割呢,最简单的就是最好的
    -----------
    离开了.Net,你怎么办?
      

  5.   

    本页页面的顶部BANNER下面有个“管理”,点这个链接。
      

  6.   

    string str1 = @"C:\我的文档\我的文件夹\cyh.txt";

    string str2=str1.Substring(0,str1.Length-7);
      

  7.   

    string str = @"C:\我的文档\我的文件夹\cyh.txt";
                int Index = str.LastIndexOf(@"\");
                string NewStr = str.Substring(0, Index+1);
                MessageBox.Show(NewStr);