John Salerno wrote:
> Hi all. I'm currently learning C#, and I'm also interested in learning
> Python

In a similar position to yourself - learning both languages - I can
definitely recommend Python ( though C# 's curly brackets might annoy
you more than they did before!!)

> so it seems like a decent
> idea to want to integrate the two

FWIW, what I've been doing lately is calling Python scripts from
Windows.Forms apps, capturing their 'StdOut'.  There's an article on
http://www.thecodeproject.com which explains how you can do this
asynchronously, but the following (C#) code is what I'm using ( imagine
a Windows Form with a TextBox to enter the script name, another to
enter any arguments for the script, and 'Run', 'Cancel' and 'Browse'
CommandButtons).


(assumes the script requires no user interaction)

        private void BrowseForScript_Click(object sender, System.EventArgs e)
        {
                if ( this.openFileDialog.ShowDialog() == DialogResult.OK )
                {
                        this.txtIO.Clear();
                        this.txtIO.Focus();
                        this.txtIO.AppendText( openFileDialog.FileName);
                }
        }

        private void Run_Click(object sender, System.EventArgs e)
        {
                System.Diagnostics.ProcessStartInfo startInfo;
                System.Diagnostics.Process process;
                string directory;
                string pyArgs;
                string script;

                script = this.txtIO.Text.Trim();

                if ( script == null || script.Length == 0 )
                {
                        return;
                }

                try
                {
                        directory = Path.GetDirectoryName( script );
                        script = Path.GetFileName( script );
                }
                catch (ArgumentException)
                {
                        MessageBox.Show("The script file path contains invalid 
characters.",
                                                        "Invalid Script Path",
                                                        MessageBoxButtons.OK,
                                                        MessageBoxIcon.Error);
                        return;
                }

                if ( script.Length == 0 )
                {
                        MessageBox.Show("No script file has been specified.",
                                                        "Invalid Script Path",
                                                        MessageBoxButtons.OK,
                                                        MessageBoxIcon.Error);
                        return;
                }

                if ( directory == null || directory.Length == 0 )
                {
                        directory = DEFAULT_SCRIPT_DIRECTORY;
                }

                pyArgs = this.txtArgs.Text.Trim();

                startInfo = new ProcessStartInfo("python");
                startInfo.WorkingDirectory = directory;
                startInfo.Arguments = script + " " + pyArgs;
                startInfo.UseShellExecute = false;
                startInfo.CreateNoWindow = true;
                startInfo.RedirectStandardOutput = true;
                startInfo.RedirectStandardError = true;

                process = new Process();
                process.StartInfo = startInfo;
                process.Start();

                string s;
                while ((s = process.StandardOutput.ReadLine()) != null)
                {
                        this.__application.Write( s + "\n" );
                }

                while ((s = process.StandardError.ReadLine()) != null)
                {
                        this.__application.Write( s + "\n" );
                }
        }


Gerard

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to