Solved

Problem executing a simple command line statement using C#

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now