Solved

Problem executing a simple command line statement using C#

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQl help with selection 14 44
Default parameter problem in C# 3 27
Word Template Mail merge with vb.net 4 40
Data Saving(2) 2 25
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 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

19 Experts available now in Live!

Get 1:1 Help Now