【问题标题】:The given path's format is not supported. when passing path what kind of path we have to pass不支持给定路径的格式。当通过路径时,我们必须通过什么样的路径
【发布时间】:2013-11-22 05:45:26
【问题描述】:

我必须在 c 驱动器的特定路径上写入文件,我使用以下代码给出上述错误不支持给定路径的格式。

我的路径值为 D:\Ea\10\rep\Demo.txt

 System.IO.File.WriteAllText(path, string.Empty);
        StreamWriter file2 = new StreamWriter(path, true);
        file2.WriteLine("Demo");
 file2.Close();
if (System.IO.File.Exists(path))
System.IO.File.Copy(path, @"D:\Demo.htm", true);

【问题讨论】:

  • 我的路径值为D:\Ea\10\rep\Demo.txt
  • 不应该是@path吗?我认为将您的路径附加到符号 @ 不会有太大作用。
  • @user2996030:你能运行你的代码吗?
  • 可能有错别字:您在这里说,您的路径是“d:\eA\...”,并在评论中写下对“d:\eD\...”的访问权限被拒绝。看看那个。
  • 我认为这是 @Dylan Corriveau 答案下方 cmets 中指定的错字。

标签: c# asp.net


【解决方案1】:
System.IO.File.WriteAllText(path, string.Empty);

您不会将@ 符号附加到路径。您应该可以只输入路径值(取决于它是什么)。

E: 你刚刚在你的 cmets 中说它的 D:\Ea\10\rep\Demo.txt ,但是错误是 'D:\ED\10\Res\Demo.txt' is denied?也许是因为文件名有点不对? 尝试改变路径值

【讨论】:

  • 我想补充一点:@符号用于字符串前面,表示给定路径是完整的(照原样)'
  • 现在即将到来 对路径“D:\ED\10\Res\Demo.txt”的访问被拒绝。
  • @Dylan:我认为这可能是 OP 的错字,如果路径不退出它会抛出:找不到路径异常 IMO
  • @user2996030:显示更多代码行有助于我们解决问题。
  • @user2996030 啊好的。仍然。您可以向我们展示更多您的代码吗?可能是您在哪里定义路径以及在哪里使用它?
【解决方案2】:

我认为代码中没有问题,但是如果您的StreamWriter 没有正确处理,那么您需要面对一些问题。所以最好将您的StreamWriter 移动到using{} 块内,这样StreamWriter 将是完成写作后立即处置。

试试这个:

            String path=@"D:\Ed\10\rep\Demo.txt";
            System.IO.File.WriteAllText(path, string.Empty);
            using (StreamWriter file2 = new StreamWriter(path, true))
            {
                file2.WriteLine("Demo");
            }
            if (System.IO.File.Exists(path))
                System.IO.File.Copy(path, @"D:\Demo.htm", true);

【讨论】:

  • 我同意。您应该尽可能尝试使用 using 谓词。它可以为您完成所有的打开/关闭。
  • 谢谢,是的,一个实现 IDisposable 接口的类可以保存在 using 块中,以便在没有用户干预的情况下进行处理。
  • @user2996030:您是否拥有运行应用程序的管理员权限?
猜你喜欢
  • 2017-09-14
  • 2011-11-13
  • 2012-04-21
  • 1970-01-01
  • 1970-01-01
  • 2014-12-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多