【发布时间】:2013-10-27 11:53:27
【问题描述】:
我已经用谷歌搜索了几个小时并尝试了我找到的所有东西,但它似乎不起作用。
我的代码是:
private static long _i;
private static readonly System.Timers.Timer Timer = new System.Timers.Timer(1000);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
[DllImport("user32.dll")]
private static extern int GetWindowModuleFileName(IntPtr hWnd, StringBuilder text, int count);
static void Main()
{
Timer.Elapsed += Timer_Elapsed;
Timer.AutoReset = true;
Timer.Start();
while (Timer.Enabled)
Console.ReadLine();
}
static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("-|- - " + _i + " - -|-");
Console.WriteLine("WindowHandle: " + GetForegroundWindow());
var buff = new StringBuilder(2048);
GetWindowModuleFileName(GetForegroundWindow(), buff, 1024);
Console.WriteLine("ModuleName: " + Path.GetFileName(buff.ToString()));
Console.WriteLine("-|- - | - -|-");
_i++;
}
这段代码的输出总是这样的:
-|- - 1 - -|-
WindowHandle: 8128962
ModuleName: ConsoleApplication.vshost.exe
-|- - | - -|-
但即使我针对 Chrome 或 Fiddler 等其他应用程序,它总是会打印出我的可执行文件名或根本没有文件名。 有没有我可能遗漏的问题?
【问题讨论】:
-
你能提供更多代码吗...比如路径是什么等?
-
@Alker 什么意思?路径是什么?这应该是所有相关代码。
-
哦,我的视觉工作室出了问题……
-
我的意思是“_i”变量。没有。
-
“它似乎不起作用”:“它”是什么? 您究竟希望您的代码做什么? 在不知道问题是什么的情况下回答您的问题有点困难。请不要用其他评论回复此评论;相反,请编辑您的问题以使其更清楚。
标签: c#