Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9699

プロセスを監視して、1秒ごとに、使用中のアプリと、操作中か否かのログを書き出す、ツール

$
0
0

完全にディストピア感があるが、「プロセスを監視して、1秒ごとに、使用中のアプリと、操作中か否かのログを書き出す、ツール」を作った。

(毎日毎日、何をやったかを業務日報を記録する必要がある現場で、毎回手入力をするのがアホくさいので、「わかった。もう全部ログ差し出すから勝手に分析して」という気持ちで、ツール作った。)

ツール作ってみて、自身で計測してみたら、8時間ぐらいの労働時間中、PC操作(キーボードかマウス操作)しているのは、3-4割ぐらいだった。そんなもんかな、とは思った。

※c#のprocessクラスではなくwinapiを利用しているのは、c#の純正では、うまくプロセス名を拾えないケースがあったため

Process_Kanshi.cs
//c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe Process_Kanshi.csusingSystem;usingSystem.Diagnostics;usingSystem.Runtime.InteropServices;usingSystem.Windows.Forms;usingSystem.Collections.Generic;usingSystem.Text;publicclasslCnt{publicintcntWrk{get;set;}publicintcntSm{get;set;}publicstringPNm{get;set;}publicstringMWT{get;set;}publicoverridestringToString(){returncntWrk+"\t"+cntSm+"\t"+PNm+"\t"+MWT;}publicvoidtoWin(){this.cntWrk++;this.cntSm++;}publicvoidtoLose(){this.cntSm++;}}publicclassProcess_Kanshi{///*[DllImport("user32.dll")]publicstaticexternintGetAsyncKeyState(longvKey);[DllImport("USER32.dll",CallingConvention=CallingConvention.StdCall)]staticexternvoidSetCursorPos(intX,intY);[DllImport("kernel32.dll")]privatestaticexternIntPtrOpenProcess(uintdwDesiredAccess,boolbInheritHandle,uintdwProcessId);[DllImport("kernel32.dll")]privatestaticexternboolCloseHandle(IntPtrhandle);[DllImport("USER32.DLL")]privatestaticexternIntPtrGetForegroundWindow();[DllImport("user32.dll",SetLastError=true)]privatestaticexternintGetWindowThreadProcessId(IntPtrhWnd,outuintlpdwProcessId);[DllImport("psapi.dll",CharSet=CharSet.Ansi)]privatestaticexternuintGetModuleBaseName(IntPtrhWnd,IntPtrhModule,[MarshalAs(UnmanagedType.LPStr),Out]StringBuilderlpBaseName,uintnSize);[DllImport("user32.dll",CharSet=CharSet.Auto,SetLastError=true)]privatestaticexternintGetWindowText(IntPtrhWnd,StringBuilderlpString,intnMaxCount);[DllImport("user32.dll",CharSet=CharSet.Auto,SetLastError=true)]privatestaticexternintGetWindowTextLength(IntPtrhWnd);publicstaticvoidMain(string[]args){///**//Process[] processList;DateTimedt=DateTime.Now;stringdt_s=dt.ToString("yyyy_MM_dd");boolDtMsCr=false;boolDtKyBd=false;boolisWin=false;strings_mc="";stringlgT="";stringpT="";stringpnm0="";stringmwt0="";Dictionary<string,lCnt>prcLog;// = new Dictionary<string, lCnt>();for(;;){///***prcLog=newDictionary<string,lCnt>();for(inti=0;i<60*5;i++){//****//---------DtMsCr=false;DtKyBd=false;s_mc=Control.MousePosition.ToString();pnm0="";mwt0="";pT="";isWin=false;//---------DateTimemdt1=DateTime.Now;//processList = Process.GetProcesses();IntPtrhWnd=GetForegroundWindow();uintprocessId;GetWindowThreadProcessId(hWnd,outprocessId);if(hWnd!=IntPtr.Zero){varhnd=OpenProcess(0x0400|0x0010,false,processId);varbuffer=newStringBuilder(255);GetModuleBaseName(hnd,IntPtr.Zero,buffer,(uint)buffer.Capacity);inttextLen=GetWindowTextLength(hWnd);StringBuildertsb=newStringBuilder(textLen+1);GetWindowText(hWnd,tsb,tsb.Capacity);varprocessName=buffer.ToString().ToLower();//-----isWin=true;stringlmwt=tsb.ToString();lmwt=lmwt.Trim().Replace(" ","");lmwt=lmwt.Substring(0,Math.Min(30,lmwt.Length));pT=processName+"_"+lmwt;Console.WriteLine(pT);pnm0=processName;mwt0=lmwt;//-----}if(!isWin&&(int)hWnd!=0){Console.WriteLine("Unknown"+" _ "+hWnd);pT="Unknown";pnm0="Unknown";}if((int)hWnd==0){Console.WriteLine("LOG_OFF"+" _ "+hWnd);pT="LOG_OFF";pnm0="LOG_OFF";}for(intix=32;ix<=226;ix++){if(GetAsyncKeyState(ix)!=0){DtKyBd=true;}}if(s_mc!=Control.MousePosition.ToString()){DtMsCr=true;}//////////////##############if(!prcLog.ContainsKey(pT)){prcLog.Add(pT,newlCnt(){cntWrk=1,cntSm=1,PNm=pnm0,MWT=mwt0});}else{if(DtKyBd||DtMsCr){prcLog[pT].toWin();//Console.WriteLine( "w");}else{prcLog[pT].toLose();//Console.WriteLine( "l");}}//////////////##############DateTimemdt2=DateTime.Now;TimeSpanmms=mdt2-mdt1;intimms=(int)mms.TotalMilliseconds;//Console.WriteLine( "imms "+ imms);imms=1000-imms-10;if(imms>=1000||imms<0){imms=700;}System.Threading.Thread.Sleep(imms);}//****dt=DateTime.Now;stringdt_sl=dt.ToString("yyyy_MM_dd_HHmmss");lgT="";foreach(KeyValuePair<string,lCnt>kvpinprcLog){lgT=lgT+dt_sl+"\t"+kvp.Key+"\t"+kvp.Value+Environment.NewLine;}System.IO.File.AppendAllText(@".\"+dt_s+".txt",lgT);lgT="";pT="";pnm0="";mwt0="";DtMsCr=false;DtKyBd=false;s_mc=Control.MousePosition.ToString();}///***}///**}///*

Viewing all articles
Browse latest Browse all 9699

Trending Articles