C#提取文件名

定义一个静态成员方法,该方法用于提取文件名。比如,给定一个字符串“c:\program files\Maths\all.dat”,使用该方法即可获取文件名all.dat。自行设计程序验证上述方法正确性。

   public static string getFilename(string file)

   {

      //提示:主体中使用string类的indexof方法和substring方法

   }

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            String Strfilme = @"c:\program files\Maths\all.dat";
            Console.WriteLine(getFilename(Strfilme));
            Console.ReadKey();

        }
        public static string getFilename(string file)
        {
            return file.Substring(file.LastIndexOf('\\')+1);
        }
    }
}


 

你可能感兴趣的:(C#提取文件名)