If you need to get the program path from where you run your file:
using System.Linq; using System.Diagnostics; try { foreach (var proc in Process.GetProcesses().Where(p => !string.IsNullOrEmpty(p.MainWindowTitle)).ToList()) { Console.WriteLine(proc.MainModule.FileName); } } catch { /*Тут ловим исключения*/ }
Also without using Linq
Console.WriteLine(Process.GetCurrentProcess().MainModule.FileName);
The name of the file being run can be found out like this:
Console.WriteLine(Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName));
To list all folders through processes, you can use this method:
foreach (Process instance in Process.GetProcesses()) { try { Console.WriteLine(instance.ProcessName); Console.WriteLine(instance.MainModule.FileName); } catch (System.ComponentModel.Win32Exception w32ex) { Console.WriteLine(w32ex.Message); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
If you want to get all the process folders you can use via WMI
using (var mCollection = new ManagementClass("Win32_Process").GetInstances()) { foreach (ManagementObject process in mCollection) { Console.WriteLine((string)process["ExecutablePath"]); //Console.WriteLine(FileVersionInfo.GetVersionInfo((string)process["ExecutablePath"]).FileDescription); } }
Console.WriteLine(Process.GetProcesses()[0].MainModule.FileName.ToString());- GooliveR