How to integrate IronPython to run a embedded Python Script

42 views Asked by At

I have 2 Python Scripts embedded into my C# Application. Essentially; 1 python script is a Decode Script and the other Script is to Encode. The idea that I have is; how can I execute these python scripts using IronPython against a selected file to decode or encode the selected file? This is my current code as of now:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using System.Net;

namespace NGH_WC_Data_Extractor_V1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            listBox1.Items.Add("Decode");
            listBox1.Items.Add("Encode");

        }

        public static int DownloadFile(String remoteFilename,
                                       String localFilename)
        {
            // Function will return the number of bytes processed
            // to the caller. Initialize to 0 here.
            int bytesProcessed = 0;

            // Assign values to these objects here so that they can
            // be referenced in the finally block
            Stream remoteStream = null;
            Stream localStream = null;
            WebResponse response = null;

            // Use a try/catch/finally block as both the WebRequest and Stream
            // classes throw exceptions upon error
            try
            {
                // Create a request for the specified remote file name
                WebRequest request = WebRequest.Create(remoteFilename);
                if (request != null)
                {
                    // Send the request to the server and retrieve the
                    // WebResponse object 
                    response = request.GetResponse();
                    if (response != null)
                    {
                        // Once the WebResponse object has been retrieved,
                        // get the stream object associated with the response's data
                        remoteStream = response.GetResponseStream();

                        // Create the local file
                        localStream = File.Create(localFilename);

                        // Allocate a 1k buffer
                        byte[] buffer = new byte[12582912];
                        int bytesRead;

                        // Simple do/while loop to read from stream until
                        // no bytes are returned
                        do
                        {
                            // Read data (up to 1k) from the stream
                            bytesRead = remoteStream.Read(buffer, 0, buffer.Length);

                            // Write the data to the local file
                            localStream.Write(buffer, 0, bytesRead);

                            // Increment total bytes processed
                            bytesProcessed += bytesRead;
                        } while (bytesRead > 0);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                // Close the response and streams objects here 
                // to make sure they're closed even if an exception
                // is thrown at some point
                if (response != null) response.Close();
                if (remoteStream != null) remoteStream.Close();
                if (localStream != null) localStream.Close();
            }

            // Return total bytes processed to caller.
            return bytesProcessed;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string download = textBox1.Text;
            string name = textBox4.Text;

            int read = DownloadFile(download, name + ".data");
            Console.WriteLine("{0} bytes written", read);
            label6.Visible = true;
        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            label9.Text = listBox1.SelectedItem.ToString() + " Selected";
            SELECTPYTHONSCRIPT();
        }

        private void SELECTPYTHONSCRIPT()
        {
            if (label9.Text == "Encode Selected")
            {
                label10.Text = "Encoding Script Enabled...";
            }
            if (label9.Text == "Decode Selected")
            {
                label10.Text = "Decoding Script Enabled...";

            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            textBox5.Text = openFileDialog1.FileName.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}

I just do not know where to look since the resources to call IronPython on a script that is embedded is limited and I just need help getting pointed in the right direction.

0

There are 0 answers