0
votes

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;
        }
    }
 }
1
"I'm having issuses with the query" Please be clear about the issues you're having.phuzi
I'm sorry , i'm having issues with the queryprider
What does "issues" mean? What errors are you getting? What output do you expect? What output do you get? etc.phuzi
the output is this : System.Management.ManagementException: 'Invalid query '.prider
Please edit the question and add full exception details to it (as text).phuzi

1 Answers

0
votes

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;
        }
    }
 }