Example #1
0
        public string getPathFromProcess(MyProcess myProcess)
        {
            var wmiQueryString = "SELECT ProcessId, ExecutablePath, CommandLine, Name FROM Win32_Process where ProcessId='" + myProcess.getProcess().Id + "'";

            using (var searcher = new ManagementObjectSearcher(wmiQueryString))
                using (var results = searcher.Get())
                {
                    var query = from p in Process.GetProcesses()
                                join mo in results.Cast <ManagementObject>()
                                on p.Id equals(int)(uint) mo["ProcessId"]
                                select new
                    {
                        CommandLine = (string)mo["CommandLine"],
                    };
                    string path = null;
                    try
                    {
                        string   words = query.ElementAt(0).ToString();
                        string[] split = words.Split(new Char[] { '"', ',', '{', '}', '\n' });
                        foreach (string itm in split)
                        {
                            try
                            {
                                string ext = "";
                                path = itm;
                                ext  = System.IO.Path.GetExtension(path);

                                while (path[0] == ' ')
                                {
                                    path = itm.Remove(0, 1);
                                    ext  = System.IO.Path.GetExtension(path);
                                }
                                while (path[path.Length - 1] == ' ')
                                {
                                    path = path.Remove(path.Length - 1, 1);
                                    ext  = System.IO.Path.GetExtension(path);
                                }
                                if (File.Exists(path) && testExt(ext))
                                {
                                    return(path);
                                }
                            }
                            catch { }
                        }
                    }
                    catch
                    {
                        path = null;
                    }
                }
            return(null);
        }