Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# command-line argument with newline fails in C# but not in python. Need to make the c# work

Tags:

python

c#

Env: .NET 3.5 Visual Studio 2008 SP1, on Win XP SP3, Python 2.7. Corporate image, no admin rights.

In C# 3.5, I want to pass a parameter to a command-line program, and this parameter contains newlines. It works in Python 2.7 but not in C#.

When body contains newline, c# truncates the result, but python passes it correctly.

Python code

cmd = self.app_path + ' email -Subject "' + subject + '" -From "' + address_from + '" -To "' + address_to +'" -Body "' + body +'"'
cmd_result = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()

C# code:

string Command = "<path to executable>";                 
string arguments = "  email -From " + FromAddress + " -To " + ToAddress + " -Subject \"" + SubjectLine + "\" -Body \"" + emailBody + "\" ";
System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo(Command, arguments);
start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
start.CreateNoWindow = false;
start.UseShellExecute = true;
System.Diagnostics.Process process = System.Diagnostics.Process.Start(start);

Any idea?

Update: body in the python example and emailBody in the c# example contain the same string, as strings are represented in each language.

Update: noticed the command wasn't terminated correctly in the python code. I added + '"' at the end of line 2. The code ran as before.

Also, as you can tell, the app called sends the body (emailBody) parameter content as an email body.

like image 421
Christopher Mahan Avatar asked Nov 05 '22 12:11

Christopher Mahan


1 Answers

Sorry but I think it is the process you call that does not take the arguments correctly; it is not the C# that is involved, and as a proof of that:

  • Create a new windows form application
  • Make sure that the Main signature looks like this:

    static void Main(string[] args)
    
  • Make sure that the call of your Main Form (named Form1) looks like this:

        if (args==null)
        {
            Application.Run(new Form1()); 
        }
        else
        {
            Application.Run(new Form1(args)); 
        }
    
  • Add a textbox to your form, with the multiline property set to true

  • Add a button to your form

  • Run the application once

  • Then Put this in the code of your button1_Click function (replace TheNameOfTheCurrentApp):

        string Command = "TheNameOfTheCurrentApp";                 
        string arguments = textBox1.Text;
        System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo(Command, arguments);
        start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
        start.CreateNoWindow = true;
        start.UseShellExecute = true;
        System.Diagnostics.Process process = System.Diagnostics.Process.Start(start);        
    
  • Add this constructor to your form:

    public Form1(string[] args)
    {
        InitializeComponent();
        textBox1.Text = string.Join(" ", args);
    }
    
  • Then run your app, insert muliple lines in your textbox and click the button. The same app will appear with the textbox correctly filled with the line breaks

like image 116
GianT971 Avatar answered Nov 12 '22 22:11

GianT971