Solved

Problem executing a simple command line statement using C#

Posted on 2014-02-19
3
447 Views
Last Modified: 2014-02-19
Hi,

I am using Windows7 system and if I go to Command line and run the following command, it works fine.
C:\WINDOWS\System32\MSG [windowsusername] "Hello"

Open in new window

So, I thought of executing the same statement from a C# program and copied this code from Internet.
            System.Diagnostics.Process process = new System.Diagnostics.Process();

            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = @" /C MSG Amit.Jain Hello"; 
            process.StartInfo = startInfo;
            process.Start();
            process.WaitForExit(100);
            process.Close();
            process.Dispose();

Open in new window

When I run the above code, unfortunately nothing happens. Does anyone have any idea what is going wrong here?

Thanks in advance.
Amit
0
Comment
Question by:AmitJain001
3 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39869748
What are you expecting to see happen? Currently you have the command window style set to System.Diagnostics.ProcessWindowStyle.Hidden which means you won't see anything on screen as the window is hidden.

Secondly, you will probably need to quote the arguments. So:
 startInfo.Arguments = @" /C MSG Amit.Jain Hello"; 

Open in new window

Should be:
 startInfo.Arguments = @" /C MSG Amit.Jain \"Hello\""; 

Open in new window

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 39870537
On my system, I had to switch the "Platform Target" to "x64" in Project --> Properties --> Build for it to work.  Then your code worked with no changes at all.
0
 

Author Closing Comment

by:AmitJain001
ID: 39872500
Carl, I changed ProcessWindowStyle.Hidden to ProcessWindowStyle.Normal and used
startInfo.Arguments = @" /C MSG Amit.Jain \"Hello\"";

Open in new window

but that did not work.

And Mike, just changing the Project Properties "Platform Target" to x64 worked like a charm. Thanks a lot.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question