您的位置:首页 > 其它

字符的处理及列出文件夹中的所有文件名

2014-09-29 09:49 162 查看
using System;

using System.IO;

using System.Text;

class test



    static void Main()

    {

        //以下为替换单个字符,将输入的字符进行替换

        //Console.Write("enter a same words:");

        //String str = Console.ReadLine();

        //Char[] arr = str.ToCharArray(0,str.Length);//转换成字符数组

        //for (int i = 0; i < str.Length; i++)

        //{

        //    if (arr[i] == 'a')

        //    {

        //        arr[i] = 'w';

        //    }

        //    Console.Write(arr[i]);

        //}

        ////以下为替换一个字符串

        //Console.Write("enter words :");

        //String str = Console.ReadLine();

        //Console.WriteLine(str);

        //String newstr = str.Replace("wen", "xiong");//将str字符串中的wen转换成xiong

        //Console.WriteLine(newstr);

        //Console.WriteLine(str);

        //以下方法判断一个路径下的文件存不存在

        //if (!File.Exists(@"F:\C#study\wenfuchun.cs")) //File.Exists判断当前path路径是否存在

        //{

        //    File.Create(@"F:\C#study\wenfuchun.cs");

        //}

        //else

        //{

        //    Console.WriteLine("输入的路径已经存在");

        //}

        //以下方法取出一个文件路径中的文件名,不含后缀

        //Console.WriteLine("the next is post the path name:");

        //String pathname = Path.GetFileNameWithoutExtension(@"F:\C#study\wenfuchun.cs");

        //Console.WriteLine(pathname);

        //以下方法在控制台中列出(F:\\C#study\\)目录下的所有文件名及其后缀

        DirectoryInfo path = new DirectoryInfo(@"F:\C#study\");//(@"F:\C#study\")改成(F:\\C#study\\)是一样的

        FileInfo[] arr = path.GetFiles();

        //使用foreach输出

        //foreach (FileInfo ph in arr)

        //{

        //    Console.WriteLine(ph.Name);

        //}

        //使用for循环输出

        for (int i = 0; i < arr.Length;i++ )

        {

            Console.WriteLine("the id number {0}:{1}",i,arr[i].Name);

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: