[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 506
  • Last Modified:

Hide console when running ManagementClass

I have a GUI application that maps drives to other computers. While the code below works fine I really don't want the console to flesh out for every drive mapping. Also I searched for a solution and found similar question with an answer http://www.experts-exchange.com/Programming/Misc/Q_23506112.html?sfQueryTermInfo=1+consol+hide+managementclass but unfortunately that's not enough for me. Can someone give me more detailed answer, possibly with some code using ManagementClass that hides the console?
Appreciate it


 public static void MapDrive(String ip, String myUser, String myPass)
        {
             String cmdString = "net use \\\\"+ip+"\\C /user:" + myUser + " " + myPass;            
            ManagementClass processClass = new ManagementClass("Win32_Process");        
            object[] methodArgs = { cmdString, null, null, 0 };
            object result = processClass.InvokeMethod("Create", methodArgs);          

        }
0
Rushii
Asked:
Rushii
  • 3
  • 2
2 Solutions
 
Alexandre SimõesManager / Technology SpecialistCommented:
Try it this way:

System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("create", cmdString);
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
System.Diagnostics.Process.Start(psi);
0
 
RushiiAuthor Commented:
AlexCode,
I think you got me close..
this line
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("create", cmdString);
didn't work for me, but I found that ProcessStartInfo constructor takes filename and arguments as parameters. So I am guessing I need to run a file that has "cmdString" in it and pass Ip, myUser and myPass as parameters to batch file?.. I have never done this but will try tomorrow. I will report on my progress, shortly.
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
Hummm... I think I led you wrong mate, sorry.

If you were on a console already, the commend that you would want to execute was:
net use \\192.168.1.66 \C /user:alexcode P@ssw0rd

correct?
Assuming so, the correct code to execute it is:

String cmdString = "use \\\\"+ip+"\\C /user:" + myUser + " " + myPass;
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("net", cmdString);
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
System.Diagnostics.Process.Start(psi);


Try it and let me know.

Cheers!
0
 
RushiiAuthor Commented:
Alexcode,
well I am not sure how that would work.  I looked up constructor for ProcessStartInfo and it goes like this
public ProcessStartInfo(string fileName, sring arguments)
So what I did is I created a batch file (I had to school myself a little) like this

net use \\%1\c /user:%2 %3
where %1, %2, %3 stand for IP, username, password and used your other code to call this batch file

String args =ip+" "+myUser+" "+myPass;
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("C:\\mapdrive.bat, args);
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
System.Diagnostics.Process.Start(psi);
And it worked seamlessly. Thanks for help

0
 
RushiiAuthor Commented:
Thank you for help
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now