How to get which computer (IP and Name) accessed the file in Shared Folder in C#, My code it should fetching my system IP address and system name while file accessed by another system, How to get which computer (IP and Name) accessed the file.
Monitor network shared folder
I am trying to monitor a network shared folder using FileSystemWatcher
on Windows.
My code is below.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Net;
using System.Net.NetworkInformation;
using System.Threading.Tasks;
namespace FileAccessMonitoringSystemInNetwork
{
public partial class frmfilemonitoringpage1 : Form
{
public const String startMonitoring = "Start Monitoring";
public const String stopMonitoring = "Stop Monitoring";
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.ControlBox = true;
this.WindowState = FormWindowState.Maximized;
this.BringToFront();
}
public frmfilemonitoringpage1()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
// Create FolderBrowserDialog object.
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
// Show a button to create a new folder.
folderBrowserDialog.ShowNewFolderButton = true;
DialogResult dialogResult = folderBrowserDialog.ShowDialog();
// Get selected path from FolderBrowserDialog control.
if (dialogResult == DialogResult.OK)
{
textBox1.Text = folderBrowserDialog.SelectedPath;
Environment.SpecialFolder root = folderBrowserDialog.RootFolder;
}
}
private void btnMonitoring_Click(object sender, EventArgs e)
{
// Create a new FileSystemWatcher object.
FileSystemWatcher fsWatcher = new FileSystemWatcher();
switch (btnMonitoring.Text)
{
// Start Monitoring…
case startMonitoring:
if (!textBox1.Text.Equals(String.Empty))
{
listBox1.Items.Add("Started FileSystemWatcher Service…");
fsWatcher.Path = textBox1.Text;
// Set Filter.
fsWatcher.Filter = (textBox2.Text.Equals(String.Empty)) ? "*.*" :
textBox2.Text;
// Monitor files and subdirectories.
fsWatcher.IncludeSubdirectories = true;
// Monitor all changes specified in the NotifyFilters.
fsWatcher.NotifyFilter = NotifyFilters.Attributes |
NotifyFilters.CreationTime |
NotifyFilters.DirectoryName |
NotifyFilters.FileName |
NotifyFilters.LastAccess |
NotifyFilters.LastWrite |
NotifyFilters.Security |
NotifyFilters.Size;
fsWatcher.EnableRaisingEvents = true;
// Raise Event handlers.
fsWatcher.Changed += new FileSystemEventHandler(OnChanged);
fsWatcher.Created += new FileSystemEventHandler(OnCreated);
fsWatcher.Deleted += new FileSystemEventHandler(OnDeleted);
fsWatcher.Renamed += new RenamedEventHandler(OnRenamed);
fsWatcher.Error += new ErrorEventHandler(OnError);
btnMonitoring.Text = stopMonitoring;
textBox1.Enabled = false;
textBox2.Enabled = false;
}
else
{
listBox1.Items.Add("Please select folder to monitor….");
}
break;
// Stop Monitoring…
case stopMonitoring:
default:
fsWatcher.EnableRaisingEvents = false;
fsWatcher = null;
btnMonitoring.Text = startMonitoring;
textBox1.Enabled = true;
textBox2.Enabled = true;
listBox1.Items.Add("Stopped FileSystemWatcher Service…");
break;
}
}
// Create a new FileSystemWatcher object.
FileSystemWatcher fsWatcher = new FileSystemWatcher();
private SqlConnection con;
public void OnCreated(object sender, FileSystemEventArgs e)
{
// Add event details in listbox.
string usernam = System.Windows.Forms.SystemInformation.UserName.ToString();
string Hostname = Dns.GetHostName().ToString();
string IPAddress = Dns.GetHostByName(Hostname).AddressList[0].ToString();
this.Invoke((MethodInvoker)delegate { listBox1.Items.Add(String.Format(" Action : {0} || Path :{1} || Date&Time : {2} || Hostname : {3} || Username : {4} || IPAddress : {5} ", e.ChangeType, e.FullPath, DateTime.Now.ToString(), Hostname, usernam, IPAddress)); });
}
// FileSystemWatcher – OnChanged Event Handler
public void OnChanged(object sender, FileSystemEventArgs e)
{
// Add event details in listbox.
String usernam = System.Windows.Forms.SystemInformation.UserName.ToString();
string Hostname = Dns.GetHostName().ToString();
string IPAddress = Dns.GetHostByName(Hostname).AddressList[0].ToString();
this.Invoke((MethodInvoker)delegate { listBox1.Items.Add(String.Format(" Action : {0} || Path :{1} || Date&Time : {2} || Hostname : {3} || Username : {4} || IPAddress : {5} ", e.ChangeType, e.FullPath, DateTime.Now.ToString(), Hostname, usernam, IPAddress)); });
}
// FileSystemWatcher – OnRenamed Event Handler
public void OnRenamed(object sender, RenamedEventArgs e)
{
// Add event details in listbox.
String usernam = System.Windows.Forms.SystemInformation.UserName.ToString();
string Hostname = Dns.GetHostName().ToString();
string IPAddress = Dns.GetHostByName(Hostname).AddressList[0].ToString();
this.Invoke((MethodInvoker)delegate { listBox1.Items.Add(String.Format(" Action : {0} || Path :{1} || Date&Time : {2} || OldName : {3} || Newname : {4} || Hostname : {5} || Username : {6} || IPAddress : {7} ", e.ChangeType, e.FullPath, DateTime.Now.ToString(), e.OldName, e.Name, Hostname, usernam, IPAddress)); });
}
// FileSystemWatcher – OnDeleted Event Handler
public void OnDeleted(object sender, FileSystemEventArgs e)
{
// Add event details in listbox.
String usernam = System.Windows.Forms.SystemInformation.UserName.ToString();
string Hostname = Dns.GetHostName().ToString();
string IPAddress = Dns.GetHostByName(Hostname).AddressList[0].ToString();
this.Invoke((MethodInvoker)delegate { listBox1.Items.Add(String.Format("Action :{0} || Path :{1} || Date&Time : {2} || Hostname : {3} || Username : {4} || IPAddress : {5} || Deleted Filename :{6}", e.ChangeType, e.FullPath, DateTime.Now.ToString(), Hostname, usernam, IPAddress, e.Name)); });
}
// FileSystemWatcher – OnError Event Handler
public void OnError(object sender, ErrorEventArgs e)
{
// Add event details in listbox.
this.Invoke((MethodInvoker)delegate { listBox1.Items.Add(String.Format("Error : {0}", e.GetException().Message)); });
}
}
}