我必须在我使用的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);发布于 2013-11-22 13:49:00
System.IO.File.WriteAllText(path, string.Empty);您不会将@符号附加到路径。您应该能够只输入路径值(取决于它是什么)。
E:你在你的评论中说它的D:\Ea\10\rep\Demo.txt,但是错误是'D:\ED\10\Res\Demo.txt‘被拒绝了?也许是因为文件名有点错吧?尝试更改路径值
发布于 2013-11-22 14:08:06
我认为在代码中没有问题,但是如果你的StreamWriter没有被正确地处理,那么你需要面对一些issues.so,最好把你的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);https://stackoverflow.com/questions/20137856
复制相似问题