views:

148

answers:

1

I need to be able to install a program in a shared folder on a computer in LAN.

First i must find out which folders are shared on a computer, and then check if there is enough disk space for installation to proceed.

Here is my method.

public static void FindShares()
    {
        try
        {
            ConnectionOptions options = new ConnectionOptions();
            options.Authentication = AuthenticationLevel.PacketPrivacy;
            options.Impersonation = ImpersonationLevel.Impersonate;
            string path = "\\\\COMPUTERNAME\\root\\cimv2";
            ManagementScope scope = new ManagementScope(path, options);

            scope.Connect();
            ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Share");

            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
            ManagementObjectCollection queryCollection = searcher.Get();

            foreach (ManagementObject m in queryCollection)
            {
                // Display shared folder information

                Console.WriteLine("Share Name : {0}", m["Name"]);
                Console.WriteLine("Share Path : {0}", m["Path"]);
                Console.WriteLine("AccessMask: {0}", m["AccessMask"]);
                Console.WriteLine("Type: {0}", m["Type"]);
                Console.WriteLine("Status : {0}", m["Status"]);
                Console.WriteLine();
            }

            string line;
            line = Console.ReadLine();
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
        }
    }

When i run this i get this error: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

I think i must set up my imperonation diferently, but i don't know how.

Thank you for your help.

A: 

Your access denied is because the WMI provider is impersonating you when it connects to the remote machine, and you're not an administrator on the remote machine.

Is the PC you are running this code on, and the PC you're trying to hit part of a Windows domain?

Is your user account directly or indirectly a member of the local administrators group on the target computer?

IanT8