简体   繁体   English

不支持给定路径的格式。 当通过路径时,我们必须通过哪种路径

[英]The given path's format is not supported. when passing path what kind of path we have to pass

i have to write file on perticuler path of c drive i used following code it is givin above error The given path's format is not supported. 我必须在c驱动器的perticuler路径上写入文件,我使用了以下代码,它在错误的上面是给定的。不支持给定路径的格式。

My path Value is D:\\Ea\\10\\rep\\Demo.txt 我的路径值是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);
System.IO.File.WriteAllText(path, string.Empty);

You wouldn't append a @ symbol to the path. 您不会在路径后附加@符号。 You should be able to just put in the path value (depending on what it is). 您应该能够只输入路径值(取决于它的大小)。

E: You just said in your comments its D:\\Ea\\10\\rep\\Demo.txt , but the error is 'D:\\ED\\10\\Res\\Demo.txt' is denied? E:您刚刚在评论中说了它的D:\\ Ea \\ 10 \\ rep \\ Demo.txt,但是错误是'D:\\ ED \\ 10 \\ Res \\ Demo.txt'被拒绝了吗? Maybe its because the file names are a bit off? 也许是因为文件名有点不对吗? trying changing the path value 尝试更改路径值

I think there is no problem in the code but if your StreamWriter is not disposed properly then you need to face some issues.so it is good to move your StreamWriter inside using{} block so StreamWriter willbe disposed as soon as it finishes the Writing. 我认为代码中没有问题,但是如果您的StreamWriter处置不当,那么您将面临一些问题。因此,最好using{}块将StreamWriter移入内部,以便在完成编写后立即处置StreamWriter

Try This: 尝试这个:

            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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM