2013-09-01 31 views
0

我正在爲我的程序創建一個日誌記錄機制,我必須將數據記錄到文件中,文件的位置和名稱由用戶指定。將文本行粘貼到記事本中(如果用戶打開它的話)

我做使用流寫入日誌如下 -

StreamWriter writer = Writer ?? File.AppendText(FileName); 

       writer.WriteLine(DateTime.Now + " " + text); 
       writer.Flush(); 
       writer.Close(); 

但是,如果用戶我的程序運行時打開該文件,新登錄線條不可見的用戶,直到用戶關閉並重新打開文件,是他們的任何方式,我可以克服這種行爲,用戶也可以看到文件打開時新登錄的行。

回答

1

實測值使用FindWindow函數的溶液和SendMessage消息下面是same-

[DllImport("user32.dll", EntryPoint = "FindWindow")] 
     private static extern IntPtr FindWindow(string lp1, string lp2); 
     [DllImport("User32.dll", CharSet = CharSet.Auto)] 
     extern static IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, [In] string lpClassName, [In] string lpWindowName); 

     [DllImport("User32.dll", EntryPoint = "SendMessage")] 
     extern static int SendMessageGetTextLength(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); 

     [DllImport("User32.dll")] 
     public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam); 

     [DllImport("User32.dll")] 
     public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, int lParam); 

    // Writes text line into log file. 
     // If the log file is opened with notepad, it is pasted there instead 
     static public void Log(string inputText, DateTime startTime = new DateTime()) 
     { 

      try 
      { 

      /*  DateTime now = DateTime.Now; 
       inputText = now.ToString() + " " + inputText; 
       if (startTime != new DateTime()) 
       { 
        double diffTime = (int)(now - startTime).TotalMilliseconds; 
        String t; 
        if (diffTime >= 10000) 
         t = ((int) diffTime/10000).ToString() + " s"; 
        else 
         t = ((int)diffTime).ToString() + " ms"; 

        inputText += ", " + t; 
       } 
*/ //irrelevant to the answer 

       // search for log file opened with notepad 
       IntPtr editBox = GetNotepadWindow(); 
       if (editBox == IntPtr.Zero) 
       { 
        // log file is not open => append line to file 
        try 
        { 
         var writer = new StreamWriter(LogFile, true); 
         writer.WriteLine(inputText); 
         writer.Flush(); 
         writer.Close(); 
        } 
        catch 
        { 
        } 
        return; 
       } 
       // paste line into notepad 
       int length = SendMessageGetTextLength(editBox, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero);     
       SendMessage(editBox, EM_SETSEL, length, length); // search end of file position 

       inputText += "\r\n"; 
       SendMessage(editBox, EM_REPLACESEL, 1, inputText); // append new line 
      } 
      catch 
      { 
      } 
     } 

     static private IntPtr GetNotepadWindow() 
     { 
      string windowName = LogFile; 
      if (string.IsNullOrEmpty(LogFile)) 
       windowName = "Untitled"; 

      int index = windowName.LastIndexOf('\\'); 
      if (index >= 0) 
       windowName = windowName.Substring(index+1); 

      IntPtr mainWindow = FindWindow("Notepad", windowName + " - Notepad"); 
      IntPtr editBox = IntPtr.Zero; 

      if (mainWindow != IntPtr.Zero) 
       editBox = FindWindowEx(mainWindow, new IntPtr(0), "Edit", null); 

      return editBox; 
     } 
代碼
相關問題