Afzaal Ahmad Zeeshan
Afzaal Ahmad Zeeshan

Reputation: 15860

NotSupportedException gets thrown

I am using C# to create a function to write data to a file in File System, and reate it and repeat function if file doesn't exist.

The first method of just writing the data is working. But the second method, if the file is not present and the program has to first create it, isn't working. It creates the file but at the very same time throws an exception to me in Visual Studio saying

System.NotSupportedException is unhandled

I am using the exact copy of code that the MSDN is having.

http://msdn.microsoft.com/en-us/library/d62kzs03(v=vs.110).aspx

Here is what I am using in my second code block,

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

The method declaration (if required) is as

private static void createFile (string fileName) {
   string file = "C:\\Users\\AfzaalAhmad\\Documents\\" + fileName + ".txt";
   /* two methods here */
}

The Image is as: (Note that there is no error in the path of file) I have used

Console.Write(file); // to get the path, and its OK!

See it in the image below ↓

enter image description here

Please note, that it does create the file in the Documents folder. But throws this exception. What am I doing wrong here?

Upvotes: 0

Views: 2063

Answers (2)

ClickRick
ClickRick

Reputation: 1569

Note the detail in the exception report: "The given path's format is not supported."

Also look at the contents of your variable file - it appears to be @"C:\Users\AfzaalAhmad\Documents\C:\Users..." - i.e. it contains the path twice.

So even though the operating system might have managed somehow to create some sort of file, the filename does not contain a valid value.

[edit] createFile(file); and Console.Write(file); are both taking the value @"C:\Users\AfzaalAhmad\Documents\dsg b.txt" but your method createFile is then adding the path a second time. Change it to simply:

private static void createFile (string file) {
   /* two methods here */
}

Upvotes: 2

NCC-1701-M
NCC-1701-M

Reputation: 261

Please have a exact look at the Exception message and the value of your file variable! There is your error!

Upvotes: 0

Related Questions