Eamonn McEvoy
Eamonn McEvoy

Reputation: 8986

FileStream throwing UnauthorisedAccessException - C#

I am trying to create a FileStream but keep getting "UnauthorisedAccessException", what is wrong with this statement?

FileStream fs = new FileStream(@"C:\", FileMode.Create, FileAccess.ReadWrite);

Thanks,

Eamonn

Upvotes: 0

Views: 1025

Answers (3)

user658006
user658006

Reputation: 41

I think the path should be a filename - "c:\test.txt" rather than the location.

Also watch that c:\ is available, depending on the OS it can be protected (eg in Vista/Win 7)

Upvotes: 3

Ben
Ben

Reputation: 35663

You are trying to open a directory as a file.

Upvotes: 4

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174457

You are not allowed to open a file stream that points to the root directory of your C partition. I assume, that's not what you want to do. If you want to create a file, than specify a file name.

Upvotes: 3

Related Questions