将输入字符串转化为合法的文件名称

将输入字符串转化为合法的文件名称

using
 System;
using System.Text;

namespace ConvertFilenameToValid
{
    //z 2012-3-5 13:17:35 PM IS2120@CSDN
    class Program
    {
        static void PrintUsage ()
        {
            System.Console.WriteLine ("Usage : ");
            System.Console.WriteLine (System.Reflection.Assembly.GetEntryAssembly ().GetName ().Name + " filename");
        }

        static void OutputValidFilename (StringBuilder fileName)
        {
            foreach (char c in System.IO.Path.GetInvalidFileNameChars ())
            {
                fileName = fileName.Replace (c, '_');
            }

            System.Console.WriteLine (fileName);
        }

        //z 2012-3-5 13:17:35 PM IS2120N
        static void OutputValidFilename (string _fileName)
        {
            StringBuilder fileName = new StringBuilder (_fileName);

            OutputValidFilename (fileName);
        }

        static void Main (string[] args)
        {
            if (args.Length != 1)
            {
                PrintUsage ();

                return;
            }

            OutputValidFilename (args[0]);

            return;
        }
    }
}

//z 2012-3-5 13:17:35 PM IS2120@CSDN
posted @ 2012-03-05 13:17  BiG5  阅读(248)  评论(0编辑  收藏  举报