Click here to Skip to main content
15,909,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I found a simple console code that gets the NTFS permissions


static void DumpSecurity(string path)

{

FileSecurity sec = File.GetAccessControl(path);

if (sec == null)
{
Console.WriteLine("File not found.");

return;
};

foreach (FileSystemAccessRule rule in sec.GetAccessRules(true, true, typeof(NTAccount)))
{

StringBuilder bldr = new StringBuilder();

if (rule.AccessControlType == AccessControlType.Deny)

bldr.Append("[DENY] ");

if (rule.IsInherited)

bldr.Append("[INHERITED] ");

bldr.AppendFormat("{0} ", rule.IdentityReference);

bldr.Append(rule.FileSystemRights);

Console.WriteLine(bldr.ToString());

};


};

What I am trying to do now is to use the code in a windowsForm application

added a listBox and a button

-The function is triggered by a button event
DumpSecurity(path);

so my problem is I cannot see the listbox within the DumpSecurity() function. In other words, trying to replace the

Console.WriteLine(bldr.ToString());
by
listBox1.Items.Add(bldr.ToString());

how can I make the listbox accessible (global) inside the function?
many thanks

What I have tried:

programmatically create a listbox inside the function, not a clean solution
Posted
Updated 7-Jul-17 5:38am

1 solution

That's because the DumpSecurity function is static - which means that it can only access static members of the class.
The Listbox is part of your Form, so it's an instance object rather than static and you cannot access it at all without passing the correct instance of the form to the function.

Alternatively, remove the static keyword from the function, assuming it's in the same Form class as the Listbox and it can access the controls.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900