Passing multiple arguments using the .Net StartInfo object

Posted on 2011-05-02
Last Modified: 2012-05-11
I'm trying to run a Python script from C# using the Process class.  Preferably, the script would contain one or more classes, each with def's.  Even without that, I would need to pass both the name of the script and a version name argument.

The closest code I can find to accomplish the latter of these possibilities is:

                    string pyscript = "../../";
                    ProcessStartInfo sInfo = new ProcessStartInfo(@"C:\\Python26\\ArcGIS10.0\\python.exe");
                    string x = "pyscript.ToString() childVersion.VersionName";
                    sInfo.Arguments = x;
                    Process proc = new Process();
                    proc.StartInfo = sInfo;

The Python script will further process childVersion.VersionName.

Now this won't work because both pyscript.ToString() and childVersion.VersionName are already strings.

More broadly, Im not clear how the compiler would sort out the arguments beyond the script name.  For example, suppose that my arguments statement was:
string x = "pyscript.ToString() myClass myDef childVersion.VersionName";

how would the program know that myClass is a class name and myDef is a method name?

Finally, what's the best way to account for gaps (e.g., "C:\\Program Files") when passing arguments?

I'm using .Net 2008 on a Windows XP SP3 OS.  The Python version is 2.6.
Question by:cshore12
    LVL 43

    Assisted Solution

    I don't know Python, so I can't supply code.

    Paths with spaces - typically one would enclose the path inside quote marks eg.  'C:\\program files\\my app\\xyz'

    Multiple arguements.  Here you have to code something in the recipient to pull them apart, you specify some sort of separator and, optionally, some ID for the type of arguement being passed.


    'First argument - second argument - third argument'   here strip out the - to leave the three arguments being passed


    -N'name' -P'path' -X'x option'
    -N'name' -X'x option' -P'path'
    here your app will identify the arguements based on the ID passed as the first two chars, so it is not requiring a fixed format for the string and parameters can be optional.
    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    You're building your arguments string wrong...


        string x = "pyscript.ToString() childVersion.VersionName";


        string x = pyscript.ToString() + " " + childVersion.VersionName;

    I don't understand what you want to do with "myClass" and "myDef" though.  Are those literals?...if not, can you show their variable declarations?

    Author Comment

    myClass and myDef are not literals; they refer to the class name and definition I would use in the Python script.  So, for example, my input string may include:

    str x = ... + "Math" + " " + "Add"

    I would like to set up the actual Python script so that it would start with:

    Class Math
       def Add:


    1) If I use Andy's approach to provide individual IDs, how do I code the script know that these arguments represent the class and definition?
    2) Alternatively, if I use the space separator as you suggest, can I refer to the argument list (e.g., argv[3], argv[4]) so that the script recognizes these arguments as class and def, respectively?

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    I don't know Python at all so I can't help you on that side.

    You are correct, though, in that you either use the absolute position (index number) or have to parse each individual argument and determine what it is "tagged" as.

    Accepted Solution

    The simple way to do it: using the Process class, pass in your arguments (Process.StartInfo.Arguments) as a string separated by a space delimiter.  Then, place a calling routine for the function you're calling within the Python script itself.  The Python function will correctly identify the arguments.

    Author Closing Comment

    Other commenters were only tangentially addressing the question.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
    Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now