Search code examples
c#klocwork

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

Solution

  • 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");
       }
    }