Jump to content

Running command line with Process.Start()

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
serpant

serpant

    Newbie

  • Members
  • PipPip
  • 14 posts
K, the code below is a complete mess since i've been messing with it to try and get it to run the most simple commands in a command prompt. But if you look at the bottom you will see all my latest attempts to get it working, the rest of the code is commented out for the most part. I've looked around on the internet, tried some different ways you can't see here, but i havent got it yet.

What it does! This program tries to mount an encrypted file using a wordlist file and truecrypt. I have forgotten my password and have already built a custom wordlist generator that i think should be able to generate the password. It combines arrays, IE

first part could be, this this or this
second part could be, this this or this

and it goes one, taking the variables, then generates all possible combinations. I just need this to apply it. I thought this was gonna take 20 minutes to write :confused: I can't seem to figure out how it works, even after looking at examples online.

namespace ConsoleApplication9
{
    class Program
    {
        static void Main(string[] args)
        {
           //System.IO.StreamReader file = new System.IO.StreamReader("list.txt");

        //    string line;
            string encfile;

            encfile = Console.ReadLine();
            


/*
            while ((line = file.ReadLine()) != null)
            {
                System.Diagnostics.Process.Start("truecrypt.exe" +
                    "/q background /s /p " + line + " /e /m ro /m rm /v " + encfile);


               
            }
            */

          System.Diagnostics.Process.Start("CMD.exe","/C \"C:\\program files\\truecrypt\\truecrypt.exe\" /q background /s /p 111qqqaaa /e /m ro /m rm /v License.txt");

          System.Diagnostics.Process.Start("CMD.exe", "msconfig");

      //  Console.WriteLine("\"C:\\program files\\truecrypt\\truecrypt.exe\" /q background /s /p 111qqqaaa /e /m ro /m rm /v License.txt");

            
            Console.ReadLine();

        }
    }
}

Edited by serpant, 07 August 2009 - 05:21 AM.
forgot code tags


#2
Parabola

Parabola

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 331 posts
maybe try this?
System.Diagnostics.Process proc = new System.Diagnostics.Process();

proc.EnableRaisingEvents=false;

proc.StartInfo.FileName="msconfig";

proc.StartInfo.Arguments="";

proc.Start();

proc.WaitForExit();

Programmer (n): An organism that can turn caffeine into code.
Programming would be so much easier without all the users.

#3
Parabola

Parabola

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 331 posts
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.EnableRaisingEvents=false;
            proc.StartInfo.FileName="c:\\Program files\\truecrypt\\truecrypt.exe";
            proc.StartInfo.Arguments="/q background /s /p 111qqqaaa /e /m ro /m rm /v License.txt";
            proc.Start();
            proc.WaitForExit();

check this:

Shell Commands within C#
Programmer (n): An organism that can turn caffeine into code.
Programming would be so much easier without all the users.

#4
serpant

serpant

    Newbie

  • Members
  • PipPip
  • 14 posts
Yeah, that makes a lot of sense! I was thinking i wouldn't be able to send the pass and argument as an option for some reason. Thanks. I'll try it right now.

I'm still curios as to why the method i was trying to use wouldn't work. IE sending string string argument directly to process.start(). But hopefully this works, i'll post back.

#5
serpant

serpant

    Newbie

  • Members
  • PipPip
  • 14 posts
This method starts the program in GUI mode all set up with the variables. I think i might have tried something like this earlier. I really need to start a console, then run the commands in it. Because if i run truecrypt then the options you see in the code above will mount if its right and do nothing if its wrong(in command prompt). In GUI mode, everything is click driven. I dont want to just start the program, i want to control and run the command prompt from my program. Hopefully this explains what i need more. Thank you for help!! I thought i had it there for a second.

I looked for ways to do this, and sending a string string process.start(), seems to be a valid option.

I tried this i believe once already

how to run command prompt commands from c# programming - ASP.NET Forums

And this is what ive spent more of my effort trying to figure out

CodeProject: Run a Command Line Tool as a Windows Application in C#. Free source code and programming help

#6
Parabola

Parabola

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 331 posts
yea, the program on the link does GUI, but the code I sent you as you can see I modified. You could easily do a console.Readline() to get commands from the console.
When I ran the script, it ran with no GUI, just ran the program that was coded to it.
Programmer (n): An organism that can turn caffeine into code.
Programming would be so much easier without all the users.

#7
serpant

serpant

    Newbie

  • Members
  • PipPip
  • 14 posts
I copied your code, which was pretty much the same as some stuff i tried before. It doesn't mount anything though, yet if i just copy the options right from VisualStudio's and run it in a command prompt, it does. I noticed this before, that's why i've been trying to figure out how to run stuff in CMD.exe.

What i've seen are a few web sties that say you can do this

Process.Start("CMD.exe","command line stuff you want to run here");

its seems so simple but it's not working!

These forums are not a lively as they used to be, eh? Last time when i asked for help on my RPG game. There was a bunch of people with ideas!! Thanks for trying parabola. I'm gonna try to figure it out a little later, then I might try another forum, it just been me and you in this one for over a day. I'd like a more active one, you know any?

#8
Parabola

Parabola

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 331 posts
I still don't think you are quite getting it.
Process.Start("CMD.exe","command line stuff you want to run here");
You should be trying to run CMD.exe with args. Actually run your line instead.
For example,
System.Diagnostics.Process.Start("ipconfig");
Or
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.EnableRaisingEvents = false;
            proc.StartInfo.FileName = "ipconfig";
            proc.StartInfo.Arguments = "";
            proc.Start();
            proc.WaitForExit();
NOT
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.EnableRaisingEvents = false;
            proc.StartInfo.FileName = "cmd.exe";
            proc.StartInfo.Arguments = "ipconfig";
            proc.Start();
            proc.WaitForExit();
Think about it- cmd is only command line. It's not the actual program you want to run, is it?
So why try to call one program, then in that programs arguments, call another, with more arguments for the second? It's inefficient. Just call your program straight in, like this:
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.EnableRaisingEvents=false;
            proc.StartInfo.FileName="c:\\Program files\\truecrypt\\truecrypt.exe";
            proc.StartInfo.Arguments="/q background /s /p 111qqqaaa /e /m ro /m rm /v License.txt";
            proc.Start();
            proc.WaitForExit();

Programmer (n): An organism that can turn caffeine into code.
Programming would be so much easier without all the users.

#9
serpant

serpant

    Newbie

  • Members
  • PipPip
  • 14 posts
What i was saying, is that i tried your code, and it doesn't work. It just starts the program and doesn't attempt to mount file. I've tried several times. I'm not sure why it doesn't work.

If i was to run truecrypt in a command line with the same options, then it does mount the file. That is why i'm trying to do it the way i am. Your way would be fine if it worked.

Not sure if it would be that much more efficient though. Once truecypt is running, operating it from command line won't start the program again, it will just try to mount the drive. So doing it your way if it worked, would be starting truecrypt a few hundred times would it not? This code is gonna be part of a loop with the password section changing each time.