Question: 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: 
[Here](https://support.roguewave.com/documentation/klocwork/en/10-x/cs.nps/) 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");
   }
}

```
According to the Klocwork [documentation for that message](https://support.roguewave.com/documentation/klocwork/en/10-x/cs.nps/) (Google is your friend!), it's because... you don't explicitly set permissions on your stream before using it.

What this means is: you are potentially creating a file, but you're not explicitly setting security information on the file, which often allows anyone to access its content. Klocwork thinks that you should explicitly set that information when you create the file (so you can follow the [principle of least privilege](https://en.wikipedia.org/wiki/Principle_of_least_privilege)).

One way of doing that would be this:

```
using (var stream = new FileStream("d:\\test.blob", FileMode.Create))
{
    var security = new System.Security.AccessControl.FileSecurity();
    security.AddAccessRule(new FileSystemAccessRule(@"domain\user", FileSystemRights.Modify, AccessControlType.Allow));
    stream.SetAccessControl(security);

    using (var streamWriter = new StreamWriter(stream))
    {
        // ...
    }
}

```