【问题标题】:Why am in getting an " Illegal characters in path" error?为什么会出现“路径中的非法字符”错误?
【发布时间】:2013-01-08 20:32:09
【问题描述】:

我正在尝试将数据保存到这样的文件中:

FileStream file = new FileStream("c:\temp", FileMode.Create, System.IO.FileAccess.Write);
        byte[] bytes = new byte[file.Length];
        file.Read(bytes, 0, (int)file.Length);
        file.Write(bytes, 0, bytes.Length);
        file.Close();
        file.Close();

我得到这个错误:

路径中有非法字符。 说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.ArgumentException:路径中有非法字符。

我做错了什么?

【问题讨论】:

    标签: asp.net-mvc-3


    【解决方案1】:

    试试 "c:\\temp" - "c:\temp" 是字符串 [c] [:] [tab character] [e] [m] [p] 这几乎肯定不是你想要的文件名!

    【讨论】:

      【解决方案2】:

      使用以下任一方法

      FileStream file = new FileStream(@"c:\temp", FileMode.Create, System.IO.FileAccess.Write);
      FileStream file = new FileStream("c:\\temp", FileMode.Create, System.IO.FileAccess.Write);
      

      '\'是转义字符,不能直接使用

      【讨论】:

        【解决方案3】:

        您需要转义文件路径中的反斜杠:

         FileStream("c:\\temp", ...
        

        \t 是制表符。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-03-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-13
          相关资源
          最近更新 更多