各位高手,请教C#如何获取文件名(不带扩展名的),比如,Q.jpg文件,我只想要Q

解决方案 »

  1.   

    System.IO.Path.GetFileNameWithoutExtension
      

  2.   

       string s = Path.GetFileNameWithoutExtension(@"F:\abc.zip");
      

  3.   

    System.IO.Path.GetFileNam(filePath)       //返回带扩展名的文件名 
    System.IO.Path.GetFileNameWithoutExtension(filePath)     //返回不带扩展名的文件名 
    System.IO.Path.GetDirectoryName(filePath)     //返回文件所在目录 
      

  4.   

    还可以
    string fullFileName = System.IO.Path.GetFileName(filePath);
    string fileName = fullFileName.LastIndexOf(".");
      

  5.   

    更正
    string fullFileName = System.IO.Path.GetFileName(filePath);
    int position = fullFileName.LastIndexOf(".");
    string fileName = fullFileName.Substring(0,position-1)
      

  6.   

    string fullname = "Q.jpg";
    string[] arraryFile;
    arraryFile = fullname.Split('.');
    string filename = arraryFile[0];Response.Write(filename);
      

  7.   

    或者:
    string fullname = "Q.jpg";
    string filename = fullname.Substring(0,fullname.LastIndexOf("."));Response.Write(filename);
      

  8.   

    System.IO.Path.GetFileNameWithoutExtension(filePath) //返回不带扩展名的文件名  这个需要引用命名空间  Using System.IO;
      

  9.   

    这个一般用在上传图片的时候。如果不上传就用数组split('.')截取字符串即可例如6楼回答的那个string fullname = "Q.jpg";
    string[] arraryFile;
    arraryFile = fullname.Split('.');
    string filename = arraryFile[0];Response.Write(filename);
      

  10.   

    这种图片一般上传图片或文件使用。如果不是上传你可以像6楼那样用split截取数组
    string str="xxxxxxx.jpg"
    string[] aa;
    aa= str.Split('.');
    string 文件名 = arraryFile[0];//你要的文件名