Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I have already known about using System.Diagnostic.Process to execute cmd.exe with arguments (dos cmd), but is that all? I would like to know if there is any other way to execute a dos cmd in Csharp and if possible in VB.Net as well. (I remember that we could use Shell command in Vb6.0 and wonder if Vb.Net has an upgraded version?).
I have also known about using an extended procedure in Sql Server to execute Dos cmd, so please let me know only new ways that I don't know.
 
Thank you very much!
Posted 17-Dec-11 8:14am
Comments
Amir Mahfoozi at 17-Dec-11 13:24pm
   
What is your reason to search for another alternation ?
King Boy at 18-Dec-11 0:25am
   
My point of view is always try doing a thing in as many ways as possible. Then you can realize the nature of the thing.
And the simpler and easier way should be found as soon as possible.
Thank you!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

VB.Net has a shell function just like VB did, but why would you settle for so limited call? With process you have (almost) all possibilities, like getting stdout, stderr, writing to stdin, defining process specific parameters and so on.
 
In sql server you have xp_cmdshell, but if the need to execute OS commands is regular and the commands are predefined I would embed a C# DLL inside the database and use Process class in that assembly to do the OS specific portion.
  Permalink  
Comments
SAKryukov at 17-Dec-11 21:05pm
   
Agree, a 5; there is a bit more to it -- please see my answer.
--SA
King Boy at 17-Dec-11 23:44pm
   
I am a little curious about embedding a dll (containing Process class) into the database and wonder:
1. Why do we have to embed such a dll? I think we can use Process normally by adding a reference to System.Diagnostic.
2. How do you wrap Process class into that dll? (I would create a new class inheriting Process class).
3. How do you embed the dll into database and extract the entire dll from the database? (I would use Serialize and Deserialize).
Thank you so much for the new trick.
Mika Wendelius at 18-Dec-11 2:41am
   
What I meant using dll with SQL Server is that I understood that you want to execute a OS command for example from a stored procedure. XP_cmdshell is the native way to do it, but you can also use custom DLL's. For example you could create an assembly that has a class and a method called CopySingleFile. After registering this to SQL Server you can call the method from SQL.
 
For example, see: http://msdn.microsoft.com/en-us/library/ms345106.aspx[^]
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

First of all, there is no such thing as "DOS Command" in any OS named "Windows". It was available when Window was a graphical environment over DOS. Whatever you run now, is a regular Win32 or 64-bit Windows application, fully-fledged, in protected mode, etc. So, there is no way to "run a DOS command". Shell command is not a DOS command, not even close.
 
To start any process the way Shell does, simply use System.Diagnostics.Process.Start, see http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^].
 
—SA
  Permalink  
Comments
King Boy at 18-Dec-11 0:11am
   
Thank you for reminding me about the concept of DOS cmd.
I think the reason I (and many others) call those commands DOS commands are:
1. The interface of cmd prompt application is most similar to DOS interface (normally with white letters on black background).
2. The similarity in executing the same functionality commands, for example, we can execute 'copy' cmd with arguments in cmd prompt as well as in DOS in the same way.
3. DOS in fact is a collection of applications (exe and com) and other resources. Cmd.exe is only one application helping us execute cmds most of which are to run other applications with arguments via cmd prompt interface. (e.g: copy.exe, shutdown.exe ...).
I mean there are many similarities between cmd prompt and DOS. I think such commands in cmd prompt simulate the corresponding commands in DOS, although they are not the same as you reminded.
Thank you so much!
SAKryukov at 18-Dec-11 0:41am
   
You are welcome. Your observations are correct and not random, explained by the efforts to preserve inheritance of products by Microsoft. I think you understand no worse than I do that similarity does not mean "the same thing".
 
Cheers,
--SA
Mika Wendelius at 18-Dec-11 2:37am
   
Good explanation, 5'd
SAKryukov at 18-Dec-11 11:57am
   
Thank you, Mika.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can use WMI. Here is one example.
 
 
 public bool CreateMailBox( string MachineName, string UserName, string Password, string domain, string domainController,string identity, string alias, string database)
         {
             string db = database.Replace(" ", "::");
             
 
             string DriveLetter = ConfigurationManager.AppSettings["EXEDriveLetter"];
             ManagementPath myPath = new ManagementPath();
             myPath.NamespacePath = @"root\CIMV2";
 
             ConnectionOptions oConn = new ConnectionOptions();
             oConn.Username = UserName;
             oConn.Password = Password;
             oConn.EnablePrivileges = true;
             myPath.Server = MachineName;
 
             ManagementScope scope = new ManagementScope(myPath, oConn);
             scope.Connect();
             ManagementPath path2 = new ManagementPath();
             path2.Server = MachineName;
             path2.ClassName = "Win32_Process";
             path2.NamespacePath = @"root\CIMV2";
             ManagementScope scopeProcess = new ManagementScope(path2, oConn);
 
             using (ManagementClass process = new ManagementClass(scopeProcess, path2, null))
             {
                 // string commandLine = @"cmd /C C:\setperm.bat " + TempName +" "+"0909020";
                 string commandLine = @"cmd /C CreateMailBox.exe " + domain +" "+domainController+" "+identity+" "+alias+" "+db+" "+UserName+" "+Password;
 
                 using (ManagementBaseObject inParams = process.GetMethodParameters("Create"))
                 {
                     inParams["CommandLine"] = commandLine;
                     inParams["CurrentDirectory"] = DriveLetter + @":\\";
                     inParams["ProcessStartupInformation"] = null;
                     using (ManagementBaseObject outParams = process.InvokeMethod("Create", inParams, null))
                     {
                         int retVal = Convert.ToInt32(outParams.Properties["ReturnValue"].Value);
 
                         return (retVal == 0);
                     }
                 }
             }
                                    
            
         }
 

notice the commandLine . Here you pass your own command line command to execute. 
 
  Permalink  
Comments
King Boy at 18-Dec-11 0:18am
   
Wow! I feel very excited with your solution using WMI which I have never known about before.
Thank you very much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 320
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 Sergey Alexandrovich Kryukov 184
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 17 Dec 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100