我正在使用C#创建一个函数来将数据写入文件系统中的文件,如果不存在文件,则读取它并重复函数。
只写数据的第一种方法是有效的。但是第二种方法,如果文件不存在,程序必须首先创建它,则无法工作。它创建文件,但同时在Visual中抛出一个异常
System.NotSupportedException未被处理
我正在使用MSDN所拥有的代码的确切副本。
http://msdn.microsoft.com/en-us/library/d62kzs03(v=vs.110).aspx
下面是我在第二个代码块中使用的内容,
// Create file!
using (FileStream fs = File.Create(file))
{
Byte[] info = new UTF8Encoding(true).GetBytes("some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
// Continue again with the request.
createFile(file);方法声明(如果需要的话)如下
private static void createFile (string fileName) {
string file = "C:\\Users\\AfzaalAhmad\\Documents\\" + fileName + ".txt";
/* two methods here */
}该图像为:(请注意,文件路径中没有错误),我使用了
Console.Write(file); // to get the path, and its OK!在↓下面的图片中看到它

请注意,它确实创建了文档文件夹中的文件。但却抛出了这个异常。我在这里做错什么了?
发布于 2014-05-24 07:54:39
注意异常报告中的细节:“不支持给定路径的格式。”
还请查看变量file的内容--它似乎是@“C:\Users\AfzaalAhmad\Documents\C:\Users.”--即它两次包含路径。
因此,即使操作系统可能以某种方式创建了某种类型的文件,但文件名并不包含有效值。
编辑createFile(file);和Console.Write(file);的值都是@"C:\Users\AfzaalAhmad\Documents\dsg b.txt“,但是您的方法createFile第二次添加了路径。将其更改为:
private static void createFile (string file) {
/* two methods here */
}发布于 2014-05-24 07:56:34
请仔细查看异常消息和文件变量的值!这就是你的错误!
https://stackoverflow.com/questions/23842562
复制相似问题