Repository navigation
Expand file tree
/
Copy pathNetDbg.cs
More file actions
126 lines (116 loc) · 4.64 KB
/
Copy pathNetDbg.cs
File metadata and controls
126 lines (116 loc) · 4.64 KB
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
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
public class NetDbgGUI
{
[STAThread]
public static void Main()
{
try
{
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
Directory.SetCurrentDirectory(baseDir);
string pythonExe = FindPythonExecutable(baseDir);
if (string.IsNullOrEmpty(pythonExe))
{
MessageBox.Show(
"Не удалось найти интерпретатор Python!\n\n" +
"Проверены пути:\n" +
"- " + Path.Combine(baseDir, "python") + "\n" +
"- " + Path.Combine(baseDir, ".venv") + "\n" +
"- " + Path.Combine(baseDir, "venv") + "\n" +
"- Системный PATH\n\n" +
"Поместите Python в папку 'python' или создайте виртуальное окружение '.venv'.",
"NetDbg - Ошибка запуска",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
return;
}
string mainScript = Path.Combine(baseDir, "main.py");
if (!File.Exists(mainScript))
{
MessageBox.Show(
"Файл main.py не найден в директории:\n" + baseDir,
"NetDbg - Ошибка",
MessageBoxButtons.OK,
MessageBoxIcon.Error
);
return;
}
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = pythonExe,
Arguments = string.Format("\"{0}\" --gui", mainScript),
WorkingDirectory = baseDir,
UseShellExecute = false,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden
};
string pythonDir = Path.GetDirectoryName(pythonExe);
if (!string.IsNullOrEmpty(pythonDir) && Directory.Exists(pythonDir))
{
psi.EnvironmentVariables["PYTHONPATH"] = baseDir;
psi.EnvironmentVariables["PATH"] = pythonDir + ";" + Environment.GetEnvironmentVariable("PATH");
}
Process proc = Process.Start(psi);
if (proc == null)
{
MessageBox.Show("Не удалось запустить процесс графического интерфейса.", "NetDbg", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
MessageBox.Show("Критическая ошибка при запуске NetDbg GUI:\n\n" + ex.Message, "NetDbg Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private static string FindPythonExecutable(string baseDir)
{
// 1. Поиск во встроенной папке python\
string localPython = Path.Combine(baseDir, "python");
if (Directory.Exists(localPython))
{
string pw = Path.Combine(localPython, "pythonw.exe");
string p = Path.Combine(localPython, "python.exe");
if (File.Exists(pw)) return pw;
if (File.Exists(p)) return p;
}
// 2. Поиск в виртуальных окружениях
string[] venvNames = new string[] { ".venv", "venv", "env" };
foreach (string vname in venvNames)
{
string venvScripts = Path.Combine(baseDir, vname, "Scripts");
if (Directory.Exists(venvScripts))
{
string pw = Path.Combine(venvScripts, "pythonw.exe");
string p = Path.Combine(venvScripts, "python.exe");
if (File.Exists(pw)) return pw;
if (File.Exists(p)) return p;
}
}
// 3. Системный pythonw / python
try
{
ProcessStartInfo checkPsi = new ProcessStartInfo
{
FileName = "where",
Arguments = "pythonw",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
};
using (Process p = Process.Start(checkPsi))
{
string output = p.StandardOutput.ReadLine();
p.WaitForExit();
if (!string.IsNullOrEmpty(output) && File.Exists(output.Trim()))
{
return output.Trim();
}
}
}
catch { }
return "pythonw";
}
}