C# - 未处理的异常 - 在路径非法字符 [英] C# - Unhandled Exception - Illegal Characters in Path

查看:161
本文介绍了C# - 未处理的异常 - 在路径非法字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚测试一些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"

这篇关于C# - 未处理的异常 - 在路径非法字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆