C#提取文件名

来源:互联网 发布:比较骚的网络女主播 编辑:程序博客网 时间:2024/05/20 10:56

定义一个静态成员方法,该方法用于提取文件名。比如,给定一个字符串“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);        }    }}


 

0 0
原创粉丝点击