|
1 | 1 | using static EZCodeLanguage.Parser; |
2 | 2 | using static EZCodeLanguage.Interpreter; |
| 3 | +using Timer = System.Timers.Timer; |
3 | 4 | using System.Data; |
4 | 5 | using System.Diagnostics; |
5 | 6 | using System.Timers; |
6 | | -using Timer = System.Timers.Timer; |
| 7 | +using Microsoft.Win32; |
| 8 | +using System.Reflection; |
7 | 9 |
|
8 | 10 | namespace EZCodeLanguage |
9 | 11 | { |
@@ -557,7 +559,7 @@ public bool IsType(object obj, object type) |
557 | 559 | throw; |
558 | 560 | } |
559 | 561 | } |
560 | | - public object ArrayParse(object array, object separator) => ObjectParse(array, "list").ToString().Split(StringParse(separator)).Select(x => x.Trim()).ToArray(); |
| 562 | + public object ArrayParse(object array, object separator) => ObjectParse(array, "list").ToString().Split(StringParse(separator)).Select(x => x.Trim()).ToArray(); |
561 | 563 | public string ArrayStringParse(object array) => ObjectParse(array, "list", to_string: false, arraySeperator: ", ").ToString(); |
562 | 564 | public int ArrayLength(object array) |
563 | 565 | { |
@@ -824,6 +826,7 @@ public static T GetParameter<T>(object obj, string type) |
824 | 826 |
|
825 | 827 | return (T)e.ObjectParse(obj, type); |
826 | 828 | } |
| 829 | + public static Exception ThrowError(string message) => ThrowError(new Exception(message)); |
827 | 830 | public static Exception ThrowError(Exception exception) |
828 | 831 | { |
829 | 832 | Error = exception.Message; |
@@ -1175,5 +1178,197 @@ private void Timer_Elapsed(object? sender, ElapsedEventArgs e) |
1175 | 1178 | _Timers[timer] = true; |
1176 | 1179 | } |
1177 | 1180 | public Dictionary<Timer, bool> _Timers = []; |
| 1181 | + |
| 1182 | + // OS Package: |
| 1183 | + // include os |
| 1184 | + public void RegistrySetKey(object _keyName, object _key, object _value) |
| 1185 | + { |
| 1186 | + try |
| 1187 | + { |
| 1188 | + string keyName = StringParse(_keyName), |
| 1189 | + key = StringParse(_key); |
| 1190 | + object value = ObjectParse(_value, "str"); |
| 1191 | + |
| 1192 | + using (var v = Registry.CurrentUser.CreateSubKey(keyName)) |
| 1193 | + { |
| 1194 | + Type type = value.GetType(); |
| 1195 | + if (type == typeof(int)) |
| 1196 | + { |
| 1197 | + value = $"_ {value}"; |
| 1198 | + } |
| 1199 | + v.SetValue(key, value); |
| 1200 | + } |
| 1201 | + } |
| 1202 | + catch (Exception ex) |
| 1203 | + { |
| 1204 | + throw ThrowError(ex); |
| 1205 | + } |
| 1206 | + } |
| 1207 | + public object RegistryGetKey(object _keyName, object _key) |
| 1208 | + { |
| 1209 | + try |
| 1210 | + { |
| 1211 | + string keyName = StringParse(_keyName), |
| 1212 | + key = StringParse(_key); |
| 1213 | + |
| 1214 | + using (var v = Registry.CurrentUser.OpenSubKey(keyName)) |
| 1215 | + { |
| 1216 | + if (v != null) |
| 1217 | + { |
| 1218 | + return v.GetValue(key); |
| 1219 | + } |
| 1220 | + else |
| 1221 | + { |
| 1222 | + throw new Exception($"Could not find registry key at '{Path.Combine(keyName, key)}'"); |
| 1223 | + } |
| 1224 | + } |
| 1225 | + } |
| 1226 | + catch (Exception ex) |
| 1227 | + { |
| 1228 | + throw ThrowError(ex); |
| 1229 | + } |
| 1230 | + } |
| 1231 | + public string RegistryLocalMachine() => Registry.LocalMachine.ToString(); |
| 1232 | + public string RegistryCurrentUser() => Registry.CurrentUser.ToString(); |
| 1233 | + public string RegistryUsers() => Registry.Users.ToString(); |
| 1234 | + public string RegistryClassesRoot() => Registry.ClassesRoot.ToString(); |
| 1235 | + public string RegistryPerformanceData() => Registry.PerformanceData.ToString(); |
| 1236 | + public string RegistryCurrentConfig() => Registry.CurrentConfig.ToString(); |
| 1237 | + public Process ProcessStart(object _path, object? _arguments) |
| 1238 | + { |
| 1239 | + try |
| 1240 | + { |
| 1241 | + string path = FixDirPath( StringParse(_path) ); |
| 1242 | + object? arguments = Try(() => StringParse(_arguments)); |
| 1243 | + |
| 1244 | + if (arguments != null) |
| 1245 | + { |
| 1246 | + return Process.Start(path, arguments.ToString()); |
| 1247 | + } |
| 1248 | + else |
| 1249 | + { |
| 1250 | + return Process.Start(path); |
| 1251 | + } |
| 1252 | + } |
| 1253 | + catch (Exception ex) |
| 1254 | + { |
| 1255 | + throw ThrowError(ex); |
| 1256 | + } |
| 1257 | + } |
| 1258 | + public string ProcessName(object _process) => (ObjectParse(_process, "process") as Process).ProcessName; |
| 1259 | + public Process ProcessGetCurrentProcess() => Process.GetCurrentProcess(); |
| 1260 | + public Process ProcessGetProcessById(object _id, object? _machineName) |
| 1261 | + { |
| 1262 | + try |
| 1263 | + { |
| 1264 | + int id = IntParse(_id); |
| 1265 | + object? machineName = Try(() => StringParse(_machineName)); |
| 1266 | + |
| 1267 | + if (machineName != null) |
| 1268 | + { |
| 1269 | + return Process.GetProcessById(id, machineName.ToString()); |
| 1270 | + } |
| 1271 | + else |
| 1272 | + { |
| 1273 | + return Process.GetProcessById(id); |
| 1274 | + } |
| 1275 | + } |
| 1276 | + catch (Exception ex) |
| 1277 | + { |
| 1278 | + throw ThrowError(ex); |
| 1279 | + } |
| 1280 | + } |
| 1281 | + public Process[] ProcessGetProcesses(object? _machineName) |
| 1282 | + { |
| 1283 | + try |
| 1284 | + { |
| 1285 | + object? machineName = Try(() => StringParse(_machineName)); |
| 1286 | + |
| 1287 | + if (machineName != null) |
| 1288 | + { |
| 1289 | + return Process.GetProcesses(machineName.ToString()); |
| 1290 | + } |
| 1291 | + else |
| 1292 | + { |
| 1293 | + return Process.GetProcesses(); |
| 1294 | + } |
| 1295 | + } |
| 1296 | + catch (Exception ex) |
| 1297 | + { |
| 1298 | + throw ThrowError(ex); |
| 1299 | + } |
| 1300 | + } |
| 1301 | + public Process[] ProcessGetProcessByName(object _name, object? _machineName) |
| 1302 | + { |
| 1303 | + try |
| 1304 | + { |
| 1305 | + string name = StringParse(_name); |
| 1306 | + object? machineName = Try(() => StringParse(_machineName)); |
| 1307 | + |
| 1308 | + if (machineName != null) |
| 1309 | + { |
| 1310 | + return Process.GetProcessesByName(name, machineName.ToString()); |
| 1311 | + } |
| 1312 | + else |
| 1313 | + { |
| 1314 | + return Process.GetProcessesByName(name); |
| 1315 | + } |
| 1316 | + } |
| 1317 | + catch (Exception ex) |
| 1318 | + { |
| 1319 | + throw ThrowError(ex); |
| 1320 | + } |
| 1321 | + } |
| 1322 | + public void ProcessKill(object _process) => (ObjectParse(_process, "process") as Process).Kill(); |
| 1323 | + public object? ProcessGetProperty(object _process, object _property) |
| 1324 | + { |
| 1325 | + try |
| 1326 | + { |
| 1327 | + var process = ObjectParse(_process, "process") as Process; |
| 1328 | + var property = StringParse(_property); |
| 1329 | + |
| 1330 | + if (process == null) |
| 1331 | + { |
| 1332 | + throw new ArgumentNullException(nameof(_process), "The process object is null."); |
| 1333 | + } |
| 1334 | + |
| 1335 | + PropertyInfo propertyInfo = typeof(Process).GetProperty(property, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); |
| 1336 | + if (propertyInfo == null) |
| 1337 | + { |
| 1338 | + throw new ArgumentException($"Property \"{property}\" not found on type \"Process\"."); |
| 1339 | + } |
| 1340 | + |
| 1341 | + return propertyInfo.GetValue(process); |
| 1342 | + } |
| 1343 | + catch (Exception ex) |
| 1344 | + { |
| 1345 | + throw ThrowError(ex); |
| 1346 | + } |
| 1347 | + } |
| 1348 | + public object? ProcessRunFunction(object _process, object _function) |
| 1349 | + { |
| 1350 | + try |
| 1351 | + { |
| 1352 | + var process = ObjectParse(_process, "process") as Process; |
| 1353 | + var function = StringParse(_function); |
| 1354 | + |
| 1355 | + if (process == null) |
| 1356 | + { |
| 1357 | + throw new ArgumentNullException(nameof(_process), "The process object is null."); |
| 1358 | + } |
| 1359 | + |
| 1360 | + MethodInfo methodInfo = typeof(Process).GetMethod(function, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); |
| 1361 | + if (methodInfo == null) |
| 1362 | + { |
| 1363 | + throw new ArgumentException($"Method \"{function}\" not found on type \"Process\"."); |
| 1364 | + } |
| 1365 | + |
| 1366 | + return methodInfo.Invoke(process, null); |
| 1367 | + } |
| 1368 | + catch (Exception ex) |
| 1369 | + { |
| 1370 | + throw ThrowError(ex); |
| 1371 | + } |
| 1372 | + } |
1178 | 1373 | } |
1179 | 1374 | } |
0 commit comments