Hello everyone, I'm Kruvazör.
I would like to thank @Helmsys for their long-standing support in the Python part of this project.
The use of various programming languages together is a common occurrence in today's modern software development processes.
Therefore, I have conducted a study on integrating C# and Python languages and in this article, we will look at IronPython, which enables C# and Python to work together seamlessly, and undertake a project.
IronPython official site
IronPython preserves the main features of the Python language while providing integration on the C# side. This combination enables the use and interaction of C# and Python code together.
For example, you can create a Python module called from C# or vice versa, create a C# class called from Python.
You can also process data entered in a C# form in Python or vice versa.
IronPython is built on the Dynamic Language Runtime (DLR). DLR provides a communication platform that enables dynamically written languages to operate more effectively.
Now that we understand IronPython and the general structure, we can proceed with the installations and write our code.
Firstly, let's open a WinForm project through Visual Studio.
Next, let's download the packages.
Now, let's write our form code.
1 Label
4 textboxes
1 button
Now, let's write our Python code.
I would like to thank @Helmsys for their long-standing support in the Python part of this project.
The use of various programming languages together is a common occurrence in today's modern software development processes.
Therefore, I have conducted a study on integrating C# and Python languages and in this article, we will look at IronPython, which enables C# and Python to work together seamlessly, and undertake a project.
IronPython official site
IronPython preserves the main features of the Python language while providing integration on the C# side. This combination enables the use and interaction of C# and Python code together.
For example, you can create a Python module called from C# or vice versa, create a C# class called from Python.
You can also process data entered in a C# form in Python or vice versa.
IronPython is built on the Dynamic Language Runtime (DLR). DLR provides a communication platform that enables dynamically written languages to operate more effectively.
Now that we understand IronPython and the general structure, we can proceed with the installations and write our code.
Firstly, let's open a WinForm project through Visual Studio.
Next, let's download the packages.
Now, let's write our form code.
1 Label
4 textboxes
1 button
Now, let's write our Python code.
Kod:
import base64
originalString = str(originalString)
print("Orjinal metin: " + originalString)
encodedString = base64.b64encode(originalString.encode()).decode()
print("Şifreli metin: " + encodedString)
decodedString = base64.b64decode(encodedString).decode()
print("Şifresi açık metin: " + decodedString)
encodedString, decodedString
Now, let's write our C# code to retrieve the data sent to Python.
Let's define it in the global area like this:
Let's define it in the global area like this:
Kod:
private ScriptEngine pythonEngine; //python engine
Kod:
public Form1()
{
InitializeComponent();
pythonEngine = Python.CreateEngine();
// kütüphanelerin kurulu olduğu dizini gösteriyoruz ki kütüphaneleri bulsun.
var searchPaths = pythonEngine.GetSearchPaths();
searchPaths.Add("..\\..\\Lib");
pythonEngine.SetSearchPaths(searchPaths);
}
Kod:
private void button1_Click(object sender, EventArgs e)
{
string originalString = textBox1.Text;
// Kullanıcıdan gelen bilgiyi python tarafına yolluyoruz.
var scope = pythonEngine.CreateScope();
scope.SetVariable("originalString", originalString);
// scriptPathte belirtilen Python kodunu çalıştır
string scriptPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "base64.py"); //proje klasöründe ara
pythonEngine.ExecuteFile(scriptPath, scope);
// Get the result from Python script
string encodedString = scope.GetVariable<string>("encodedString");
string decodedString = scope.GetVariable<string>("decodedString");
// Display results in labels
textBox2.Text = "Original string: " + originalString;
textBox3.Text = "Encoded string: " + encodedString;
textBox4.Text = "Decoded string: " + decodedString;
}
After moving our Python file to the project folder, we can open the application.
Topic Owner : @Kruvazör C# ve Python Entegrasyonu ile Base64 encoder/decoder Yapımı (IronPython)
Topic Owner : @Kruvazör C# ve Python Entegrasyonu ile Base64 encoder/decoder Yapımı (IronPython)
Son düzenleme: