Search code examples
c#.netwmiwmi-queryget-wmiobject

local path of a share using wmi c#


I'm trying to make an .exe , that can list me the physical path(using WMI query)of a shared folder from the shared s name . I'm trying to add the name of the share to the query as a 'Filter' (where ) I'm having issues with the query and get an Exception:

System.Management.ManagementException: 'Invalid query '

Looking for someone that can help me and explain

    Static void Main(string[] args)
        {
            Console.WriteLine("ENTER SERVER NAME ");
            string serverName = Console.ReadLine();
            Console.WriteLine("ENTER SHARE FOLDER NAME");
            string shareName = Console.ReadLine();
            Dictionary<string, string> shares = GetNetworkShareDetailUsingWMI(serverName , shareName );
                foreach (KeyValuePair<string, string> share in shares)
                {
                    Console.WriteLine(share.Key + ": " + share.Value);
                    Console.WriteLine("-------------");
                
                }
    Console.ReadLine();
           ;
     }


        public static Dictionary<string, string > GetNetworkShareDetailUsingWMI(string serverName ,string  shareName  )
        {
            Dictionary<string, string> shares = new Dictionary<string, string>();

            var path = string.Format(@"\\{0}\root\cimv2", serverName);
            var query = string.Format("Select * from Win32_Share where Path likeSelect * from Win32_Share where Name like '%" shareName "%'" );
            ManagementScope scope = new ManagementScope(path);
            scope.Connect();

            ManagementObjectSearcher worker = new ManagementObjectSearcher(scope, new ObjectQuery(query));
            foreach (ManagementObject share in worker.Get())
            {
                shares.Add(share["Name"].ToString(), share["Path"].ToString());
                Console.WriteLine(shares);
            }
            Console.ReadLine();
            return shares;
        }
    }
 }

Solution

  • Solution is this: I wrong to implement the query

    class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("ENTER SERVER NAME ");
                string serverName = Console.ReadLine();
                Console.WriteLine("ENTER SHARE FOLDER NAME");
                string shareName = Console.ReadLine();
                Dictionary<string, string> shares = GetNetworkShareDetailUsingWMI(serverName , shareName );
                    foreach (KeyValuePair<string, string> share in shares)
                    {
                        Console.WriteLine(share.Key + ": " + share.Value);
                        Console.WriteLine("-------------");
                    
                    }
        Console.ReadLine();
               ;
         }
    
    
            public static Dictionary<string, string > GetNetworkShareDetailUsingWMI(string serverName ,string  shareName  )
            {
                Dictionary<string, string> shares = new Dictionary<string, string>();
    
                var path = string.Format(@"\\{0}\root\cimv2", serverName);
                var query = string.Format("Select * from Win32_Share where Name like '%{0}%'", shareName);
                ManagementScope scope = new ManagementScope(path);
                scope.Connect();
    
                ManagementObjectSearcher worker = new ManagementObjectSearcher(scope, new ObjectQuery(query));
                foreach (ManagementObject share in worker.Get())
                {
                    shares.Add(share["Name"].ToString(), share["Path"].ToString());
                    Console.WriteLine(shares);
                }
                Console.ReadLine();
                return shares;
            }
        }
     }