how to pass input or parameter using startinfo.argument?

AnkiIt_wizard5 66 Reputation points
2021-02-19T10:04:41.547+00:00

Process proc = new Process(); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.Arguments = startInfo.FileName = proc.StartInfo = startInfo; proc.Start(); anyone who know how to take input from user using startinfo.argument?

Developer technologies C#
{count} votes

Accepted answer
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-02-19T10:52:45.63+00:00

    Here are windows form examples.

    Add one argument

    var startInfo = new ProcessStartInfo("TODO") {WorkingDirectory = @"TODO"};
    
    if (!string.IsNullOrWhiteSpace(textBox1.Text))
    {
        startInfo.ArgumentList.Add(textBox1.Text);
    }
    
    Process.Start(startInfo);
    

    Adding all TextBoxes

    var startInfo = new ProcessStartInfo("TODO") {WorkingDirectory = @"TODO"};
    
    foreach (var textBox in Controls.OfType<TextBox>())
    {
        if (!string.IsNullOrWhiteSpace(textBox.Text))
        {
            startInfo.ArgumentList.Add(textBox.Text);
        }
    }
    
    Process.Start(startInfo);
    

    With spaces in text

    var startInfo = new ProcessStartInfo("TODO") {WorkingDirectory = @"TODO"};
    
    foreach (var textBox in Controls.OfType<TextBox>())
    {
        if (string.IsNullOrWhiteSpace(textBox.Text)) continue;
        if (textBox.Text.Contains("\""))
        {
            startInfo.ArgumentList.Add($"\"{textBox.Text}\"");
        }
    }
    
    Process.Start(startInfo);
    

    One by one

    var startInfo = new ProcessStartInfo("TODO") {WorkingDirectory = @"TODO"};
    
    if (!string.IsNullOrWhiteSpace(textBox1.Text))
    {
        startInfo.ArgumentList.Add($"\"{textBox1.Text}\"");
    }
    
    if (!string.IsNullOrWhiteSpace(textBox2.Text))
    {
        startInfo.ArgumentList.Add($"\"{textBox2.Text}\"");
    }
    
    Process.Start(startInfo);
    

    Variation

    var startInfo = new ProcessStartInfo("TODO") {WorkingDirectory = @"TODO"};
    
    if (!string.IsNullOrWhiteSpace(textBox1.Text))
    {
        startInfo.ArgumentList.Add(textBox1.Text.Contains("\"") ? 
            $"\"{textBox1.Text}\"" : 
            $"{textBox1.Text}");
    }
    
     Process.Start(startInfo);
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.