Solved

Problem executing a simple command line statement using C#

Posted on 2014-02-19
3
444 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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to check to see if datatable has headers or not 2 30
Printing 1 51
VB.Net Tranfer data between to VB.Net apps 8 26
DataTables + iCheck + pagination Issue 2 23
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

810 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