Console venster commands gebruiken

Status
Niet open voor verdere reacties.

badboyR

Gebruiker
Lid geworden
11 apr 2012
Berichten
28
Ik wil via een programma een ander console venster starten hierop commands uitvoeren, maar ik weet niet hoe dit moet. Ik moet drie dingen uitvoeren, en hoef niks te lezen. Kan iemand hier een voorbeeld van geven, alvast bedankt.

Code:
                        // Opstarten
                        Process spark = new Process();
                        spark.StartInfo.FileName = (Directory.GetCurrentDirectory() + @"\SparkCS.exe");
                        spark.StartInfo.UseShellExecute = false;
                        spark.StartInfo.RedirectStandardInput = true;
                        spark.StartInfo.RedirectStandardOutput = true;
                        spark.StartInfo.RedirectStandardError = true;
                        spark.StartInfo.CreateNoWindow = true;
                        spark.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                        spark.Start();

                        // Aflsuiten
                        spark.Close();
                        spark.Dispose();
 
Hoi,

In Visual Studio heb je de mogelijkheid om Console Applications te maken.
Als je zo'n applicatie hebt, kun je direct tegen een commandvenster uitvoer geven.

Console.Writeline("Dit is een voorbeeld van een geschreven lijn"); *

Zie ook:
http://msdn.microsoft.com/en-us/library/0wc2kk78(v=vs.90).aspx
Of:
http://msdn.microsoft.com/en-us/library/452fz12a(v=vs.80).aspx

Waarin uitgelegd wordt hoe console klassen precies werken.


Succes,

d0mzy


*Noot: Gebruik Console.ReadLine(); om de al uitgevoerde tekst in beeld te laten staan
(Dus dan komt er een extra enter na de laatste regel)
 
Laatst bewerkt:
Dat weet ik ook wel, maar ik moet een functie vanuit een ander programma dat niet door mij geschreven is gebruiken. En er is trouwens een confusator over het programma heen gehaald.
 
Als je dit vanaf een Windows Form Application probeert te doen. Als ik het goed begrijp hoef je geen informatie terug te krijgen vanuit dat programma? Je wilt dus simpel weg commanda's uitvoeren?

Code:
// usage
const string ToolFileName = "example.exe";
string output = RunExternalExe(ToolFileName);

public string RunExternalExe(string filename, string arguments = null)
{
    var process = new Process();

    process.StartInfo.FileName = filename;
    if (!string.IsNullOrEmpty(arguments))
    {
        process.StartInfo.Arguments = arguments;
    }

    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    process.StartInfo.UseShellExecute = false;

    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.RedirectStandardOutput = true;
    var stdOutput = new StringBuilder();
    process.OutputDataReceived += (sender, args) => stdOutput.Append(args.Data);

    string stdError = null;
    try
    {
        process.Start();
        process.BeginOutputReadLine();
        stdError = process.StandardError.ReadToEnd();
        process.WaitForExit();
    }
    catch (Exception e)
    {
        throw new Exception("OS error while executing " + Format(filename, arguments)+ ": " + e.Message, e);
    }

    if (process.ExitCode == 0)
    {
        return stdOutput.ToString();
    }
    else
    {
        var message = new StringBuilder();

        if (!string.IsNullOrEmpty(stdError))
        {
            message.AppendLine(stdError);
        }

        if (stdOutput.Length != 0)
        {
            message.AppendLine("Std output:");
            message.AppendLine(stdOutput.ToString());
        }

        throw new Exception(Format(filename, arguments) + " finished with exit code = " + process.ExitCode + ": " + message);
    }
}

private string Format(string filename, string arguments)
{
    return "'" + filename + 
        ((string.IsNullOrEmpty(arguments)) ? string.Empty : " " + arguments) +
        "'";
}
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan