Monday, 15 April 2013

c# 4.0 - get current process name with file extension in c# -



c# 4.0 - get current process name with file extension in c# -

getallproccess function homecoming runing proccesses in windows. want current proccess name extension ".avi", ".mkv", ".mpg", ".mp4", ".wmv" e.g. if play video file in windows media player homecoming (wmplayer.exe) or if play video file in km player returns(kmplayer.exe) here code code working slow reference http://vmccontroller.codeplex.com/sourcecontrol/changeset/view/47386#195318

string filename; process[] procs = process.getprocesses() ; foreach (process prc in procs) {

if (procs.length > 0) { int id = prc.id; ienumerator<filesysteminfo> fie = detectopenfiles.getopenfilesenumerator(id); while (fie.movenext()) { if (fie.current.extension.tolower(cultureinfo.invariantculture) == ".mp3") { filename = fie.current.fullname; break; // todo: might not correct. : exit while } } } }

you start taking @ handle mark russinovich. run administrator , homecoming files used processes.

you utilize next syntax set results text file:

handle.exe > log.txt

afterwards, may utilize powershell extract info processes using info files:

get-content log.txt | where{$_.readcount -gt 6} | foreach{ if($_.substring(0,1) -ne " " -and $_.substring(0,1) -ne "-") {$process = $_.tostring()} elseif($_.tolower() -like "*.avi" ` -or $_.tolower() -like "*.mkv" ` -or $_.tolower() -like "*.mpg" ` -or $_.tolower() -like "*.mp4" ` -or $_.tolower() -like "*.wmv" ` ) {$process.tostring()} }

here's same approach c# (you need run application administrator):

class programme { static void main(string[] args) { var processes = getprocesses(); // enumerate processes foreach (tuple<int,string> mediafile in processes.distinct()) { var process = process.getprocesses().where(i => i.id == mediafile.item1).firstordefault(); console.writeline("{0} ({1}) uses {2}", process.processname, process.id, mediafile.item2); } console.readline(); } private static list<tuple<int,string>> getprocesses() { string line = ""; int counter = 0; string currentprocess = ""; list<tuple<int, string>> mediafiles = new list<tuple<int, string>>(); process compiler = new process(); compiler.startinfo.filename = @"c:\yourpath\handle.exe"; compiler.startinfo.createnowindow = true; compiler.startinfo.useshellexecute = false; compiler.startinfo.redirectstandardoutput = true; compiler.start(); while ((line = compiler.standardoutput.readline()) != null) { // skipping applicaion info if (++counter > 6) { if (!" -".contains(char.parse(line.substring(0, 1)))) { currentprocess = line; } else if ((new[] { ".avi", ".mkv", ".mpg", ".mp4", ".wmv" }) .contains(line.tolower().substring(line.length - 4))) { int pos = currentprocess.indexof("pid:") + 5; string pid = currentprocess.substring(pos, currentprocess.indexof(" ", pos) - pos); mediafiles.add(new tuple<int, string>(int32.parse(pid),line.substring(21))); } } } compiler.waitforexit(); homecoming mediafiles; } }

c# c#-4.0

No comments:

Post a Comment