?
Solved

Problem executing a simple command line statement using C#

Posted on 2014-02-19
3
Medium Priority
?
491 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

741 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