Create application log file in C# / Write to File example

Application log file example
private string logFile = "full path and file name (C:\\temp\\MyApplicationLog.txt)";
...
WriteToLogFile("your application message", logFile); // use any time to add record to the log file
...
public void WriteToLogFile(string strMessage, string outputFile)
{
   string line = DateTime.Now.ToString() + " | ";
   line += strMessage;
   FileStream fs = new FileStream(outputFile, FileMode.Append, FileAccess.Write, FileShare.None);
   StreamWriter swFromFileStream = new StreamWriter(fs);
   swFromFileStream.WriteLine(line);
   swFromFileStream.Flush();
   swFromFileStream.Close();
}


|
2014/10/19 08:55

C# Is the Logged User a member of specific Windows Domain Group?

C sharp code
using System.DirectoryServices.AccountManagement;
string userName = SystemInformation.UserName;
string UDN = SystemInformation.UserDomainName;
string domainUser = UDN + "\\" + userName;
PrincipalContext pc = new PrincipalContext(ContextType.Domain);
UserPrincipal user = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, domainUser);
GroupPrincipal group = GroupPrincipal.FindByIdentity(pc, "Domain_Group_Name");
bool is_member = user.IsMemberOf(group);

|
2013/10/19 11:10

DataGridView add row - C#

Considering DataGridView with 3 text columns defined.
string[] row = new string[] { "column1_content", "column2_content", "column3_content" };
DataGridView1.Rows.Add(row);



|
2012/10/19 09:18

DataGridView copy to clipboard

To copy the content of DataGridView cells to clipboard using MouseClick function                                                       
private void DataGridView1_MouseClick(object sender, MouseEventArgs e)
{
   DataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
   DataGridView1.SelectAll();
   Clipboard.SetDataObject(DataGridView1.GetClipboardContent());
}


|
2012/10/19 09:15

Windows application (process) start from C# code

Run notepad application example
using System.Diagnostics; // REQUIRED
private string programStartupDir = "";
private string logFile = "";
...
private void Form1_Load(object sender, EventArgs e)
{
   programStartupDir = Directory.GetCurrentDirectory();
   programStartupDir += "\\";
   logFile = programStartupDir + "Application_Log.txt";
}
...
private void menuItemShowLogFile_Click(object sender, EventArgs e)
{
   Process showFile = new Process();
   showFile.StartInfo.FileName = "notepad.exe";
   showFile.StartInfo.Arguments = logFile;
   showFile.Start();
}




|
2012/10/19 09:06