Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 28th, 2009, 9:42 AM   #1
crazyguy_6
Newbie
 
Join Date: Feb 2009
Posts: 27
Rep Power: 0 crazyguy_6 is on a distinguished road
Question Cmd

How can I use Command Line Functions in C#?
Here's what I want to accomplish in Visual Basic:
shell("shutdown -s")
Thats easy enough, but how do I do it in C#?
crazyguy_6 is offline   Reply With Quote
Old Feb 28th, 2009, 10:58 AM   #2
cdingSpree
hoßßy¦st C++ Þrogrammer
 
Join Date: Sep 2008
Posts: 816
Rep Power: 10 cdingSpree will become famous soon enough
Send a message via Skype™ to cdingSpree
Re: Cmd

Try PsychoCoder's solution.
http://www.dreamincode.net/forums/showtopic33948.htm
cdingSpree is offline   Reply With Quote
Old Feb 28th, 2009, 12:06 PM   #3
Alias
Hobbyist Programmer
 
Join Date: Oct 2007
Location: Newcastle upon Tyne, UK
Posts: 113
Rep Power: 11 Alias is on a distinguished road
Send a message via MSN to Alias Send a message via Skype™ to Alias
Re: Cmd

@cdingSpree, WMI is like a time-stopping machine, use it, if you like suspended animation.

You could just use the Process class, here's an example you would need to edit as desired:

    public class CmdHelper
    {
        private readonly ProcessStartInfo 
            propStartInfo = new ProcessStartInfo("cmd");
        public CmdHelper() { }
        public CmdHelper(string args)
        {
            propStartInfo.Arguments = args;
        }

        public string Arguments
        {
            get { return propStartInfo.Arguments; }
            set { propStartInfo.Arguments = value; }
        }

        public void RunCmd()
        {
            bool result = false;
            var cmdProc = new Process();
            try
            {
                cmdProc.StartInfo = propStartInfo;
                if (cmdProc.Start())
                {
                    cmdProc.WaitForExit();
                    result = true;
                }
                else
                {
                    //handle it
                }
            }
            catch (InvalidOperationException invopEx)
            {
                //handle it
            }
            catch (Win32Exception win32Ex)
            {
                //handle it
            }
            catch (SystemException sysEx)
            {
                //handle it
            }
            catch (Exception ex)
            {
                //handle it
            }
            finally
            {
                if (cmdProc != null)
                    cmdProc.Dispose();
            }
            return result;
        }
    }

Hope this gets you started enough, as for usage:

    var cmdHelper = new CmdHelper();
    cmdHelper.Arguments = "shutdown -s";
    cmdHelper.RunCmd();
Alias is offline   Reply With Quote
Old Feb 28th, 2009, 12:32 PM   #4
cdingSpree
hoßßy¦st C++ Þrogrammer
 
Join Date: Sep 2008
Posts: 816
Rep Power: 10 cdingSpree will become famous soon enough
Send a message via Skype™ to cdingSpree
Re: Cmd

Quote:
Originally Posted by Alias View Post
@cdingSpree, WMI is like a time-stopping machine, use it, if you like suspended animation.
Didn't know that. I just googled, found a solution, posted the link. I don't even know if his/her code compiles.
cdingSpree is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
invalid character in expression Chetanji Sed and Awk 5 Dec 1st, 2008 2:46 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:24 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC