2013-11-22 100 views
0

我必须写在C驱动器的perticuler路径上的文件我使用下面的代码它是上面的错误givin不支持给定路径的格式。不支持给定路径的格式。当传递路径我们必须通过什么样的路径

我的路径值是d:\ EA \ 10 \代表\ 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); 
+0

我的路径值是d:\ EA \ 10 \代表\ Demo.txt –

+0

它不应该是'@ path'?我不认为将你的路径附加到符号@会做很多事情。 –

+0

@ user2996030:你能运行你的代码吗? –

回答

2
System.IO.File.WriteAllText(path, string.Empty); 

你不会一个@符号附加到路径。你应该能够放入路径值(取决于它是什么)。 E:你刚刚在你的评论中说过它的D:\ Ea \ 10 \ rep \ Demo.txt,但是错误是'D:\ ED \ 10 \ Res \ Demo.txt'被拒绝了吗?也许是因为文件名有点偏离? 试图改变路径值

+1

我想补充一点:@符号用于字符串前面,指定给定的路径是完好的(原样)' –

+0

现在即将到来访问路径'D:\ ED \ 10 \ Res \ Demo.txt'被拒绝。 –

+0

任何解决方案来解决这个 –

1

,我认为是没有问题的代码,但如果你的StreamWriter没有正确配置,那么你需要面对一些issues.so这是好事,将您StreamWriterusing{}块,StreamWriter将非常具有配置只要它完成写作。

试试这个:

  String [email protected]"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); 
+0

我同意。你应该尽可能地使用使用谓词。它可以为你做所有的开幕式/闭幕式。 –

+0

谢谢,是的,一个实现了IDisposable接口的类可以保存在使用块中,这样在用户不需要干预的情况下就可以保证处理。 –

+0

同样的错误是老板 –

相关问题