简体   繁体   中英

Removing from listbox an unknown value

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM