获取当前进程名称与文件扩展名在c# [英] get current process name with file extension in c#
问题描述
GetAllProccess函数返回Windows中的所有运行进程。我想获取当前进程名,其扩展名为。avi,.mkv,.mpg,.mp4,.wmv
eg如果我在Windows媒体播放器中播放任何视频文件,它返回(wmplayer.exe)或如果我在KM PLAYER中播放任何视频文件,它返回(kmplayer.exe)
感谢
这里是我的代码此代码工作速度很慢
参考 http://vmccontroller.codeplex.com / SourceControl / changeset / view / 47386#195318
GetAllProccess Function Return All Runing Proccesses In Windows. I want to get current proccess name which extension is ".avi", ".mkv", ".mpg", ".mp4", ".wmv"
e.g. if I play any video file in windows media player it return (wmplayer.exe) or if I play any video file in KM PLAYER it returns(kmplayer.exe)
Thanks
here is my code this code working very slow
reference http://vmccontroller.codeplex.com/SourceControl/changeset/view/47386#195318
string filename;
Process [] procs = Process.GetProcesses();
foreach(procs中的进程prc)
{
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 be correct. Was : Exit While
}
}
}
}
推荐答案
您可以先看看 Handle By Mark Russinovich 。
您可以使用以下语法将结果放入一个文本文件:
You could use the following syntax to put the results into a text file:
handle.exe > log.txt
之后,您可以使用 PowerShell
使用这些数据文件提取有关进程的信息:
Afterwards, you may use PowerShell
to extract the information about the processes using those data 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()}
}
这是从C#(你需要以管理员身份运行应用程序)相同的方法:
Here's the same approach from C# (you need to run the application as Administrator):
class Program
{
static void Main(string[] args)
{
var processes = GetProcesses();
// enumerate the 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();
return mediaFiles;
}
}
这篇关于获取当前进程名称与文件扩展名在c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!