2

I've listed all directories from C:\Users\ to a listbox.

listBox1.Items.AddRange(Directory.GetDirectories("C:\\Users\\", "*" , SearchOption.TopDirectoryOnly));

All Users in Windows have the folder \\AppData\\ but I don't want to mess with these folders because they have important files for windows, assuming a computer's guy using my software have 2 or more windows accounts, all these have \\AppData\\ folder, with first user I used to do:

listbox1.items.remove("C:\\Users\\" + Environment.UserName + "\\AppData\\")

but I don't know the others users name, there is any way to remove all AppData folders in listbox without knowing the username?

Cœur
  • 37,241
  • 25
  • 195
  • 267
I love Code
  • 101
  • 1
  • 7
  • why you not saving this data in registers ? – AsfK Jul 24 '17 at 05:32
  • Did you consider getting a list of subfolders from the directory (path)? Maybe that would help? :) – Sometowngeek Jul 24 '17 at 05:32
  • I agree with sometowngeek, that will solve your problem –  Jul 24 '17 at 05:34
  • " I don't want to mess with these folders because they have importante files for windows" Are your users aware of your not seeming to have a problem "messing" with other data in their `Users` folders? – oerkelens Jul 24 '17 at 05:34
  • I think `System.Manegment` could help you. https://stackoverflow.com/questions/12749537/how-can-i-get-a-list-local-windows-users-only-the-users-that-appear-in-the-wind – Ben Jul 24 '17 at 05:34

1 Answers1

0

you can get the user names using this code

class Program
{
    static void Main(string[] args)
    {

        SelectQuery query = new SelectQuery("Win32_UserAccount");
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
        foreach (ManagementObject envVar in searcher.Get())
        {
            Console.WriteLine("Username : {0}", envVar["Name"]);
        }

        Console.ReadLine();

    }
    // end of class
}

you might need to add reference for System.Management

Cyber Progs
  • 3,656
  • 3
  • 30
  • 39
  • 3
    From msdn https://msdn.microsoft.com/en-us/library/aa394507(v=vs.85).aspx `Note Because both the Name and Domain are key properties, enumerating Win32_UserAccount on a large network can negatively affect performance. Calling GetObject or querying for a specific instance has less impact.` Makes the answer not false. Just want to make sure, that the performance could be bad. – Ben Jul 24 '17 at 05:42
  • Thank you, Its worked, and I don't need time or performance, also thank you Ben. – I love Code Jul 25 '17 at 03:03