user584018 user584018 - 1 month ago 9
C# Question

No permission set for resource 's' before accessing it

I'm getting Klockwork error for below code, What could be the issue?

Note - My code compile successfully. only Klockworks gave this error.

Error,

No permission set for resource 's' before accessing it

using (StreamWriter s = new StreamWriter("CDriveDirs.txt"))
{
s.WriteLine("Hello");
}

Answer Source

Here is a good explanation for this warning.

You need to provide explicit access controls for your file.

Try the following code:

using (var fs = new FileStream(@"g:\temp_test.txt", FileMode.Append))
{
   fs.SetAccessControl(new FileSecurity(@"g:\temp_test.txt", AccessControlSections.Access));
   using (var s = new StreamWriter(fs))
   {
      s.WriteLine("Test");
   }
}