2012-12-22 50 views
0
public partial class Loginform : Form 
{  
    public Loginform() 
    { 
     InitializeComponent(); 
    } 

    private void btnlogin_Click(object sender, EventArgs e) 
    { 
     string dir = "D://Login.hhh"; 
     if (!File.Exists(dir)) 
     { 
      File.Create(dir); 
     } 

     string filePath = dir; 
     string s = System.Environment.GetEnvironmentVariable("COMPUTERNAME"); 
     string s1 = getIP(); 
     using (StreamWriter swrObj = new StreamWriter(filePath, true)) 
     { 
      swrObj.Write(s1 + "|" + s + "|" + Txtusername.Text + "|" + "user logged in on :|" + DateTime.Now.ToShortDateString() + " at " + DateTime.Now.ToShortTimeString()); 
      swrObj.Write("\t\t"); 
      swrObj.Write("\t\t"); 
      swrObj.WriteLine(); 
      MessageBox.Show("Login success"); 
     } 
    } 

    private void Loginform_Load(object sender, EventArgs e) 
    { 

    } 
    private string getIP() 
    { 
     IPHostEntry host; 
     string localIP = "?"; 
     host = Dns.GetHostEntry(Dns.GetHostName()); 
     foreach (IPAddress ip in host.AddressList) 
     { 
      if (ip.AddressFamily.ToString() == "InterNetwork") 
      { 
       localIP = ip.ToString(); 
      } 
     } 
     return localIP; 
    } 

} 

我想申請日誌文件在C#項目中,我使用上面的方法來創建日誌文件用於獲取系統名稱,IP地址和用戶登錄,但它適用於單用戶環境,如果將其應用於多用戶環境,如何獲取所有用戶的日誌信息?請任何人幫助我?如何獲得登錄多個用戶的文件在C#

+0

你要創建日誌文件,分別爲每個用戶... – Pranav

回答

0

我假設你要創建日誌文件爲每個用戶,那麼你可以做這樣的: -

private void btnlogin_Click(object sender, EventArgs e) 
    { 
     string s1 = getIP(); 
     string dir = "D://"+s1+"-"+DateTime.Now+"Login.hhh"; 
     if (!File.Exists(dir)) 
     { 
      File.Create(dir); 
     } 

     string filePath = dir; 
     string s = System.Environment.GetEnvironmentVariable("COMPUTERNAME"); 

     using (StreamWriter swrObj = new StreamWriter(filePath, true)) 
     { 
      swrObj.Write(s1 + "|" + s + "|" + Txtusername.Text + "|" + "user logged in on :|" + DateTime.Now.ToShortDateString() + " at " + DateTime.Now.ToShortTimeString()); 
      swrObj.Write("\t\t"); 
      swrObj.Write("\t\t"); 
      swrObj.WriteLine(); 
      MessageBox.Show("Login success"); 
     } 
    } 
相關問題