Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Friends i want to display the list of shared folders from a remote server in a local machine but i am getting Exception(User credentials cannot be used for local connections) Any help would be appreciated .......here is my code..
Thank U...........


C#
System.Management.ManagementScope ms1 = new System.Management.ManagementScope();
            
            ms1.Options.Username = "pavan";
            ms1.Options.Password = "welcome";

            using (System.Management.ManagementClass exportedShares = new     System.Management.ManagementClass(ms1, new ManagementPath("\\\\servername\\root\\cimv2:Win32_Share"), null))
            {
                System.Management.ManagementObjectCollection shares = exportedShares.GetInstances();
                foreach (System.Management.ManagementObject share in shares)
                {
                    Response.Write("Name: " + share["Name"].ToString());
                }
            }
Posted
Updated 30-Oct-12 3:07am
v11
Comments
bbirajdar 13-Jun-12 9:09am    
You need network credentials
B.S.S Pavan Kumar 15-Jun-12 8:37am    
Thank U for replying ..... I ve already given ntwk credentials (in line 3 and 4) Those are the uname and pwd for the system i want to get the list of shared folders....still wat are ntwk credentials should i give there plz help me out.....
bbirajdar 15-Jun-12 10:08am    
This statement of your is contradictory - "I ve already given ntwk credentials (in line 3 and 4) Those are the uname and pwd for the system i want to get the list of shared folders".. You need to provide the NETWORK credentials and not the SYSTEM credentials....Both of these are different.Only then it will work.

Have you tried without using the user credentials.. Just comment the userid pwd and try.. the following code works for me..


C#
System.Management.ManagementScope ms1 = new System.Management.ManagementScope();
           // ms1.Options.Username = "";
           // ms1.Options.Password = "";
            using (System.Management.ManagementClass exportedShares = new System.Management.ManagementClass(ms1, new ManagementPath("\\\\machinename\\root\\cimv2:Win32_Share"), null))
            {
                System.Management.ManagementObjectCollection shares = exportedShares.GetInstances();
                foreach (System.Management.ManagementObject share in shares)
                {
                    Response.Write("Name: " + share["Name"].ToString());
                }
            }
 
Share this answer
 
Comments
B.S.S Pavan Kumar 19-Jun-12 0:46am    
Yes I have already Tried it out but it Throws Access Denied Exception ()
[no name] 30-Oct-12 9:23am    
1. Are you providing correct machine name in wmi path ?
2. Try username as domain\username
[no name] 30-Oct-12 9:32am    
because i am getting output w/o credentials and providing correct servername
Try with this:

C#
ms1.Options.Authority = string.Format("NTLMDOMAIN:{0}", txtDomain.Text);
ms1.Options.Impersonation = ImpersonationLevel.Impersonate;
 
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