0

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;
        }
    }
 }
phuzi
  • 12,078
  • 3
  • 26
  • 50
prider
  • 23
  • 5
  • "I'm having issuses with the query" Please be clear about the issues you're having. – phuzi Feb 24 '21 at 10:05
  • I'm sorry , i'm having issues with the query – prider Feb 24 '21 at 10:28
  • What does "issues" mean? What errors are you getting? What output do you expect? What output do you get? etc. – phuzi Feb 24 '21 at 10:30
  • the output is this : System.Management.ManagementException: 'Invalid query '. – prider Feb 24 '21 at 10:31
  • Please edit the question and add full exception details to it (as text). – phuzi Feb 24 '21 at 10:33
  • I'm sorry for my bad explanation , i edited this . I'm trying to use the name of the share as 'input' , as output i would like to see the pshysical share of the shared folder entered as 'input' – prider Feb 24 '21 at 10:40
  • I found the solution , i implemented share name in the wrong way – prider Feb 24 '21 at 11:08

1 Answers1

0

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;
        }
    }
 }
prider
  • 23
  • 5