C# - 未处理的异常 - 在路径非法字符路径、字符、异常、未处理

2023-09-04 23:59:34 作者:终归单人心

我刚刚测试一些code的那一刻,尽管调用StartRemoveDuplicate(当它编译)抛出一个异常时,抱怨非法字符:

我的code是如下:

 类节目
    {
        静态无效的主要(字串[] args)
        {
            的foreach(在System.IO.File.ReadAllLines字符串EXENAME(的test.txt))
            {
                的Process.Start(TEST.EXE,\+ EXENAME +\)WaitForExit()。
            }

            StartRemoveDuplicate();

        }



        私有静态无效RemoveDuplicate(字符串sourceFilePath,串destinationFilePath)
        {
            VAR readlines方法= File.ReadAllLines(sourceFilePath,Encoding.Default);

            File.WriteAllLines(destinationFilePath,readLines.Distinct()的ToArray(),Encoding.Default。);
        }


        私有静态无效StartRemoveDuplicate()
        {
            RemoveDuplicate(C:\ test.txt的,C:\的test2.txt);
        }

    }
 

解决方案

尝试串像以前一样使用@:

  @C:\ test.txt的
 

或escpe的\卡拉科特

 C:\\的test.txt
 
电脑在安装软件的时候说文件路径含有非法字符怎么整的啊

I am just testing some code at the moment, although when calling the StartRemoveDuplicate (when its compiled) an Exception is thrown, complaining about illegal characters:

My code is as follows:

 class Program
    {
        static void Main(string[] args)
        {
            foreach (string exename in System.IO.File.ReadAllLines("test.txt"))
            {
                Process.Start("test.exe", "\"" + exename + "\"").WaitForExit();
            }

            StartRemoveDuplicate();

        }



        private static void RemoveDuplicate(string sourceFilePath, string destinationFilePath)
        {
            var readLines = File.ReadAllLines(sourceFilePath, Encoding.Default);

            File.WriteAllLines(destinationFilePath, readLines.Distinct().ToArray(), Encoding.Default);
        }


        private static void StartRemoveDuplicate()
        {
            RemoveDuplicate("C:\test.txt", "C:\test2.txt");
        }

    }

解决方案

Try to use @ before the string like :

@"C:\test.txt"

or to escpe the "\" caracter

"C:\\test.txt"

 
精彩推荐
图片推荐