-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
243 lines (220 loc) · 8.81 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace PythonRunner
{
public partial class Form1 : Form
{
private IniFile Settings = new IniFile(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\connorcode\PythonRunner\settings.ini");
private string[] formats = { "py", "txt", "nose", "ini", "md", "json", "csv", "gitignore" };
private string path = @"";
private string TempPath = "PyRunner.tmp";
private string TmpPath;
private string ExePath;
private string PyPath;
private string working;
private Regex rg;
private void Form1_Load(object sender, EventArgs e)
{
textBox2.Text = Settings.Read("pythonEXE");
ExePath = Settings.Read("pythonEXE");
path = Settings.Read("folderPath");
textBox3.Text = Settings.Read("folderPath");
try {checkBox1.Checked = Boolean.Parse(Settings.Read("doDebug"));} catch { checkBox1.Checked = false; }
if (path != "")
{
DirectoryInfo directoryInfo = new DirectoryInfo(path);
if (directoryInfo.Exists)
{
treeView1.AfterSelect += treeView1_AfterSelect;
BuildTree(directoryInfo, treeView1.Nodes);
}
foreach (string format in formats)
{
working += "(" + format + ")|";
}
rg = new Regex(@"\." + working.Remove(working.Length - 1));
}
}
public Form1()
{
InitializeComponent();
}
private class IniFile
{
private string Path;
private string EXE = Assembly.GetExecutingAssembly().GetName().Name;
[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern long WritePrivateProfileString(string Section, string Key, string Value, string FilePath);
[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern int GetPrivateProfileString(string Section, string Key, string Default, StringBuilder RetVal, int Size, string FilePath);
public IniFile(string IniPath = null)
{
Path = new FileInfo(IniPath ?? EXE + ".ini").FullName;
}
public string Read(string Key, string Section = null)
{
var RetVal = new StringBuilder(255);
GetPrivateProfileString(Section ?? EXE, Key, "", RetVal, 255, Path);
return RetVal.ToString();
}
public void Write(string Key, string Value, string Section = null)
{
WritePrivateProfileString(Section ?? EXE, Key, Value, Path);
}
public void DeleteKey(string Key, string Section = null)
{
Write(Key, null, Section ?? EXE);
}
public void DeleteSection(string Section = null)
{
Write(null, null, Section ?? EXE);
}
public bool KeyExists(string Key, string Section = null)
{
return Read(Key, Section).Length > 0;
}
}
private void BuildTree(DirectoryInfo directoryInfo, TreeNodeCollection addInMe)
{
try
{
TreeNode curNode = addInMe.Add(directoryInfo.Name);
foreach (FileInfo file in directoryInfo.GetFiles())
{
curNode.Nodes.Add(file.FullName, file.Name);
}
foreach (DirectoryInfo subdir in directoryInfo.GetDirectories())
{
BuildTree(subdir, curNode.Nodes);
}
}
catch { }
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
this.richTextBox1.Clear();
if (rg.Match(e.Node.Name.ToLower()).Success)
{
PyPath = e.Node.Name;
StreamReader reader = new StreamReader(e.Node.Name);
this.richTextBox1.Text = reader.ReadToEnd();
reader.Close();
}
}
private string CreateTempRun(string BaseFile)
{
StreamReader reader = new StreamReader(BaseFile);
StreamWriter writer = new StreamWriter(path + "\\" + TempPath);
writer.Write("#Sigma76's Py Runner\n" + reader.ReadToEnd() + "\ninput(\"[ PRESS RETURN TO EXIT ]\")");
writer.Close();
return path + "\\" + TempPath;
}
private void run_cmd(string cmd, string args, string ExePath)
{
string cmdArgs;
if (checkBox1.Checked)
{
cmdArgs = "/k ";
}
else
{
cmdArgs = "/c ";
}
string strCmdText = cmdArgs + ExePath + " " + cmd + " " + args;
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
}
private void button1_Click(object sender, EventArgs e)
{
if (ExePath != null)
{
string responce = CreateTempRun(PyPath);
TmpPath = responce;
run_cmd(responce, textBox1.Text, ExePath);
}
else
{
MessageBox.Show("No Python Executable!!! " + ExePath, "PyRunner", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void textBox2_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog
{
InitialDirectory = @"C:\",
Title = "Pick Python Executable",
CheckFileExists = true,
CheckPathExists = true,
DefaultExt = "exe",
Filter = "Python Executable (*.exe)|*.exe",
FilterIndex = 2,
RestoreDirectory = true,
ReadOnlyChecked = true,
ShowReadOnly = true
};
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox2.Text = openFileDialog1.FileName;
ExePath = openFileDialog1.FileName;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (File.Exists(TmpPath))
{
File.Delete(TmpPath);
}
foreach (string path in new string[] { @"\connorcode\", @"\connorcode\PythonRunner\" })
{
if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + path))
{
Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + path);
Console.WriteLine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + path);
}
}
if (!File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\connorcode\PythonRunner\settings.ini"))
{
using (File.Create(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\connorcode\PythonRunner\settings.ini")) ;
}
try
{
Settings.Write("pythonEXE", ExePath);
Settings.Write("folderPath", path);
Settings.Write("doDebug", checkBox1.Checked.ToString());
}
catch (Exception b) { Console.WriteLine(b); }
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
using (var fbd = new FolderBrowserDialog())
{
DialogResult result = fbd.ShowDialog();
path = fbd.SelectedPath;
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
{
textBox3.Text = fbd.SelectedPath;
DirectoryInfo directoryInfo = new DirectoryInfo(fbd.SelectedPath);
if (directoryInfo.Exists)
{
treeView1.AfterSelect += treeView1_AfterSelect;
BuildTree(directoryInfo, treeView1.Nodes);
}
foreach (string format in formats)
{
working += "(" + format + ")|";
}
rg = new Regex(@"\." + working.Remove(working.Length - 1));
}
}
}
private void button2_Click(object sender, EventArgs e)
{
var form2 = new Form2();
form2.Show();
}
}
}