Click here to Skip to main content
15,896,606 members
Articles / Programming Languages / C#

Net Spy: Your Network Spy

Rate me:
Please Sign up or sign in to vote.
4.31/5 (63 votes)
16 Feb 2008CPOL4 min read 205.4K   6.4K   188  
This is a Windows based tool to continuously monitor your shared folders in a network and generate a log for different folders.
/***********************************************************
 *  Title       : NetSpy 
 *  Description : It will continiously monitor our Shared 
 *                Folder and Check who is accessing and 
 *                What are changes are made by accesser
 *                and All other Details
 *      Feature : Monitoring, Log Gereation, Track Changes        
 * Technology   : C#,WMI, MQL
 * Written By   : Abhijit Jana 
 * Email        : mca_abhijit@yahoo.co.in
 * * ***********************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management; 
using System.IO;
using CustomUIControls;
using System.Data.OleDb; 

namespace NetWorkSpy
{
    public partial class Form1 : Form
    {

        public int _RefreshTime = 10;
        public string RemoteIPAddress="";
        public string RemoteUserName="";
        public string RemoteActiveTime="";
        public string totalAccessFile="";
        public string RemoteOS="";
        public bool _ShowPopUp=true;
        public int Timcnt = 0;
        public string IDealTime = "";
        public int oldcount = 0, newcount = 0;
        public string RDirPath = "";
        public static string sDBPath = @".\spyDB.mdb";
        public static string ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sDBPath + ";Persist Security Info=False;";
        public int cCount = 0;
        public int dCount = 0;
        public int cHCount = 0;
        public int rCount = 0;
        public OleDbConnection conn = new OleDbConnection();
        FileSystemWatcher[] mywatcher_List = new FileSystemWatcher[100];
        public int iWatcherCount = 0;
        public int iSharedCount = 0;
        public string[] sFileWatcherPath= new string[100];
        TaskbarNotifier taskbarNotifier1 = new TaskbarNotifier();
        Timer tim = new Timer();
        public int _CurrentActiveTab;
        public string  _CreateLogFilePath="";
        public string _DeleteLogFilePath="";
        public string _RenamedLogFilePath = "";
        public string _ChangedLogFilePath = "";

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            conn.Close();
            conn.ConnectionString = ConnectionString;  
            Auto_Refresh();            // Call AutoRefresh 
            TaskBar_Initilization();   // Initilize TaskBar Notification
            //Set The Notify Icon
            NotifyIcon.ShowBalloonTip(1);
            ListView_init();
            Read_Shared_Folder();
            Read_Current_Session();
            _CreateLogFilePath = Directory.GetCurrentDirectory() + @"\" + "Create_Log.Txt";
            _DeleteLogFilePath = Directory.GetCurrentDirectory() + @"\" + "Delete_Log.Txt";
            _RenamedLogFilePath = Directory.GetCurrentDirectory() + @"\" + "Renamed_Log.Txt";
            _ChangedLogFilePath = Directory.GetCurrentDirectory() + @"\" + "Changed_Log.Txt";
        

        }
        private void Form1_Resize(object sender, System.EventArgs e)
        {
           // Hide The Form when it's minimized
            if (FormWindowState.Minimized == WindowState)
                Hide();  
        }
        private void NotifyIcon_DoubleClick(object sender, System.EventArgs e)
        {
           // Show the form when Dblclicked on Notifyicon
            Show();
            WindowState = FormWindowState.Normal;
        }

         // Area For All Menu of System Tray icon ......

        // Click on Restore...
        private void MnuRestore_Click(object sender, EventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }
        //Click on Close...
        private void MnuClose_Click(object sender, EventArgs e)
        {

            Application.Exit();
        }
    //___________________________________________________________
        // Methods for initilizing List View Header in Shared Folder
        private void ListView_init()
        {

            // List View For Shared Folder........
            listView1.View = View.Details;
            listView1.Columns.Add("Shared", -1, HorizontalAlignment.Left);
            listView1.Columns.Add("Path", -1, HorizontalAlignment.Left);
            listView1.Columns.Add("Description", -1, HorizontalAlignment.Left);
            listView1.Columns.Add("Status", -1, HorizontalAlignment.Left);
            listView1.Columns[0].Width = 100;
            listView1.Columns[1].Width = 200;
            listView1.Columns[2].Width = 200;
            listView1.Columns[3].Width = 75; 

            // List View For Current Session
            lviewSession.View = View.Details;
            lviewSession.Columns.Add("IP Address", -1, HorizontalAlignment.Left);
            lviewSession.Columns.Add("User Name", -1, HorizontalAlignment.Left);
            lviewSession.Columns.Add("Access Time(Min)", -1, HorizontalAlignment.Left);
            lviewSession.Columns.Add("Ideal Time(Min)", -1, HorizontalAlignment.Left);
            lviewSession.Columns.Add("Remote OS", -1, HorizontalAlignment.Left);
            lviewSession.Columns[0].Width = 100;
            lviewSession.Columns[1].Width = 100;
            lviewSession.Columns[2].Width = 50;
            lviewSession.Columns[3].Width = 50;
            lviewSession.Columns[4].Width = 200;

            // List of Access Files !!!!
            lview.View = View.Details;
            lview.Columns.Add("User Name", -1, HorizontalAlignment.Left);
            lview.Columns.Add("Access Shared Folder",-1 , HorizontalAlignment.Left);
            lview.Columns[0].Width = 200;
            lview.Columns[1].Width = 200;
            // List of Created Files !!!!
            
            lstCreate.View = View.Details;
            lstCreate.Columns.Add("Created File Path", -1, HorizontalAlignment.Left);
            lstCreate.Columns.Add("Date", -1, HorizontalAlignment.Left);
            lstCreate.Columns.Add("Time", -1, HorizontalAlignment.Left);
            lstCreate.Columns[0].Width = 200;
            lstCreate.Columns[1].Width = 50;
            lstCreate.Columns[2].Width = 50; 

            // List of Deleted Files
           
            lstDelete.View = View.Details;
            lstDelete.Columns.Add("Deleted File Path", -1, HorizontalAlignment.Left);
            lstDelete.Columns.Add("Date", -1, HorizontalAlignment.Left);
            lstDelete.Columns.Add("Time", -1, HorizontalAlignment.Left);
            lstDelete.Columns[0].Width = 200;
            lstDelete.Columns[1].Width = 50;
            lstDelete.Columns[2].Width = 50; 

            //List of Renamed Files

            lstRenamed.View = View.Details;
            lstRenamed.Columns.Add("Old File Path", -1, HorizontalAlignment.Left);
            lstRenamed.Columns.Add("New File Path", -1, HorizontalAlignment.Left);
            lstRenamed.Columns.Add("Date", -1, HorizontalAlignment.Left);
            lstRenamed.Columns.Add("Time", -1, HorizontalAlignment.Left);
            lstRenamed.Columns[0].Width = 150;
            lstRenamed.Columns[1].Width = 150;
            lstRenamed.Columns[2].Width = 50; 
            lstRenamed.Columns[3].Width = 50;

            //List of Changed Files

            lstChanged .View = View.Details;
            lstChanged.Columns.Add("File Path", -1, HorizontalAlignment.Left);
            lstChanged.Columns.Add("Date", -1, HorizontalAlignment.Left);
            lstChanged.Columns.Add("Time", -1, HorizontalAlignment.Left);
            lstChanged.Columns[0].Width = 200;
            lstChanged.Columns[1].Width = 200;
            lstChanged.Columns[2].Width = 50;
            
            
        }

        private void tabShared_Click(object sender, EventArgs e)
        {

        }
        private void load_Shared_Item(int cnt,string folder,string path, string Desc,string status)
        {
            listView1.Items.Add(folder);
            listView1.Items[cnt].SubItems.Add(path);
            listView1.Items[cnt].SubItems.Add(Desc);
            listView1.Items[cnt].SubItems.Add(status);
               
        }
        private void load_Access_folder(int cnt, string username, string folder)
        {
            lview.Items.Add(username);
            lview.Items[cnt].SubItems.Add(folder);
          


            
        }
        private void Read_Shared_Folder()
        {
            int cnt = 0;
            try
            {
               // MQL for Shaer Information
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2","SELECT * FROM Win32_Share");

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    RDirPath = queryObj["Path"].ToString();
                    if (!RDirPath.Equals("") || RDirPath.Equals("IPC$") || RDirPath.Equals("ADMIN$") )
                    {
                        File_Watcher(RDirPath);
                    }
                    load_Shared_Item(cnt, queryObj["Name"].ToString(), queryObj["Path"].ToString(), queryObj["Description"].ToString(), queryObj["Status"].ToString() );
                    cnt=cnt+1;

                }
            }
            catch (ManagementException e)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
        }
        
        // Wll Load Current session Data
        private void Load_Current_Session(int cnt, string ipaddress,string username, string accesstime,string ideltime,string os)
        {
             
                    lviewSession.Items.Add(ipaddress);
                    lviewSession.Items[cnt].SubItems.Add(username);
                    lviewSession.Items[cnt].SubItems.Add(accesstime);
                    lviewSession.Items[cnt].SubItems.Add(ideltime);
                    lviewSession.Items[cnt].SubItems.Add(os);  
        }
        
        private  void Read_Current_Session()
        {
            int counter = 0;
            int ActiveTime;
            int itime;
            string sharename;
            try
            {
                ManagementObjectSearcher searcher =
                   new ManagementObjectSearcher("root\\CIMV2",
                   "SELECT * FROM Win32_ServerConnection");
                ManagementObjectSearcher searcher2 =
                   new ManagementObjectSearcher("root\\CIMV2",
                   "SELECT * FROM Win32_ServerSession"); 
                // Read the object 
                foreach (ManagementObject ServerQobj in searcher2.Get())
                {
                    RemoteOS = ServerQobj["ClientType"].ToString();
                    IDealTime = ServerQobj["IdleTime"].ToString();
                    itime = Int32.Parse(IDealTime) / 60;
                    IDealTime = itime.ToString();
                }

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    RemoteIPAddress = queryObj["ComputerName"].ToString();
                    RemoteUserName = queryObj["UserName"].ToString();
                    RemoteActiveTime = queryObj["ActiveTime"].ToString();
                    ActiveTime = (Int32.Parse(RemoteActiveTime)) / 60;
                    RemoteActiveTime = ActiveTime.ToString();
                    sharename = queryObj["ShareName"].ToString();
                    if (!sharename.Equals("IPC$"))
                    {
                        Load_Current_Session(counter, RemoteIPAddress, RemoteUserName, RemoteActiveTime,IDealTime,RemoteOS);
                        load_Access_folder(counter, RemoteUserName, sharename);
                        counter += 1;
                    }
                }
            }
            catch (ManagementException e)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
        }

        private void Monitor_Accessed_Files()
        {
          
        }
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void tbarRefresh_Click(object sender, EventArgs e)
        {
            Refresh_All();
            
        }

        private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void lviewSession_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        // ToolBar Refresh Button Click....


        //private void File_Watcher(string sFolderPath)
        //{
        //        FileSystemWatcher mywatcher = new FileSystemWatcher(sFolderPath);           
        //        mywatcher.Filter = "";
        //        mywatcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName |  NotifyFilters.LastAccess ;
        //        mywatcher.EnableRaisingEvents = true;
        //        mywatcher.IncludeSubdirectories = true;
        //        mywatcher.Created += new FileSystemEventHandler(mywatcher_created);
        //        mywatcher.Deleted += new FileSystemEventHandler(mywatcher_deleted);
        //        mywatcher.Changed += new FileSystemEventHandler(mywatcher_changed);
        //        mywatcher.Renamed += new RenamedEventHandler(mywatcher_renamed);
        //        mywatcher_List[iWatcherCount] = mywatcher;
        //        iWatcherCount++;
           
            
        //}

        private void File_Watcher(string sFolderPath)
        {
            if (Directory.Exists(sFolderPath))
            {
                FileSystemWatcher mywatcher = new FileSystemWatcher(sFolderPath);
                mywatcher.Filter = "";
                mywatcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess;
                mywatcher.EnableRaisingEvents = true;
                mywatcher.IncludeSubdirectories = true;
                mywatcher.Created += new FileSystemEventHandler(mywatcher_created);
                mywatcher.Deleted += new FileSystemEventHandler(mywatcher_deleted);
                mywatcher.Changed += new FileSystemEventHandler(mywatcher_changed);
                mywatcher.Renamed += new RenamedEventHandler(mywatcher_renamed);
                if (iWatcherCount >= 100)
                {
                    Array.Resize(ref mywatcher_List, iWatcherCount + 1);
                }
                mywatcher_List[iWatcherCount] = mywatcher;
                iWatcherCount++;
            }


        }

        protected void mywatcher_created(object sender,FileSystemEventArgs  e)
        {
           CheckForIllegalCrossThreadCalls = false;
           DateTime current = DateTime.Now;
           lstCreate.Items.Add(e.FullPath.ToString() );
           lstCreate.Items[cCount].SubItems.Add(current.ToShortDateString());
           lstCreate.Items[cCount].SubItems.Add(current.ToShortTimeString());
           cCount += 1;
          
         }
        protected void mywatcher_renamed(object sender,RenamedEventArgs r)
        {
            CheckForIllegalCrossThreadCalls = false;
            DateTime current = DateTime.Now;
          lstRenamed.Items.Add(r.OldFullPath.ToString());
          lstRenamed.Items[rCount].SubItems.Add(r.FullPath.ToString());
          lstRenamed.Items[rCount].SubItems.Add(current.ToShortDateString());
          lstRenamed.Items[rCount].SubItems.Add(current.ToShortTimeString());
          rCount += 1;
      
        }
        protected void mywatcher_deleted(object sender, FileSystemEventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
            DateTime current = DateTime.Now;
            lstDelete.Items.Add(e.FullPath.ToString());
            lstDelete.Items[dCount].SubItems.Add(current.ToShortDateString());
            lstDelete.Items[dCount].SubItems.Add(current.ToShortTimeString());
            dCount += 1;

        }
        protected void mywatcher_changed(object sender, FileSystemEventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
            DateTime current = DateTime.Now;
            lstChanged.Items.Add(e.FullPath.ToString());
            lstChanged.Items[cHCount].SubItems.Add(current.ToShortDateString());
            lstChanged.Items[cHCount].SubItems.Add(current.ToShortTimeString());
            cHCount += 1;
         }
        private void TaskBar_Initilization()
         {
           //taskbarNotifier1.SetBackgroundBitmap(new Bitmap(GetType(), "skin"), Color.FromArgb(255, 0, 255));
            taskbarNotifier1.SetBackgroundBitmap("Skin.bmp", Color.FromArgb(255, 0, 255));
            //taskbarNotifier1.SetCloseBitmap(new Bitmap(GetType(), "close"), Color.FromArgb(255, 0, 255), new Point(127, 8));
            taskbarNotifier1.SetCloseBitmap("close.bmp", Color.FromArgb(255, 0, 255), new Point(127, 8));
            taskbarNotifier1.TitleRectangle = new Rectangle(40, 9, 70, 25);
            taskbarNotifier1.ContentRectangle = new Rectangle(8, 41, 133, 68);
            taskbarNotifier1.TitleClick += new EventHandler(TitleClick);
            taskbarNotifier1.ContentClick += new EventHandler(ContentClick);
            taskbarNotifier1.CloseClick += new EventHandler(CloseClick);
        }
        void CloseClick(object obj, EventArgs ea)
        {
           // MessageBox.Show("Closed was Clicked");
        }

        void TitleClick(object obj, EventArgs ea)
        {
            //MessageBox.Show("Title was Clicked");
        }

        void ContentClick(object obj, EventArgs ea)
        {
           // MessageBox.Show("Content was Clicked");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }
        void Show_popUP()
        {
            string t1 = "500";
            string t2 = "3000";
            string t3 = "500";
            taskbarNotifier1.CloseClickable = true;
            taskbarNotifier1.TitleClickable = false;
            taskbarNotifier1.ContentClickable = true;
            taskbarNotifier1.EnableSelectionRectangle = true;
            taskbarNotifier1.KeepVisibleOnMousOver = true;	// Added Rev 002
            taskbarNotifier1.ReShowOnMouseOver = true;			// Added Rev 002
            taskbarNotifier1.Show("NetSpy", RemoteUserName + "\n Is Now Accessing Your System ", Int32.Parse(t1), Int32.Parse(t2), Int32.Parse(t3));
        }

        private void Auto_Refresh()
        {
            tim.Start();
            tim.Tick+=new EventHandler(tim_Tick); 
        }
      void tim_Tick(object obj, EventArgs ea)
        {
            Timcnt+=1;
            newcount = lviewSession.Items.Count;
           if (oldcount != newcount)
            {
              
                oldcount = newcount;
            }
            if (Timcnt > 100*_RefreshTime)
            {
                
                Refresh_All();
                oldcount = newcount; 
                Timcnt = 0;
            }
        }
        private void Refresh_All()
        {
            int len;

            
            len = RemoteUserName.Length;
            if(len>0 )
            {
               if(_ShowPopUp) 
                     Show_popUP();
         
            }
            for (int i = 0; i < iWatcherCount; i++)
                 mywatcher_List[i].Dispose();

            listView1.Items.Clear();
            lviewSession.Items.Clear();
            lview.Items.Clear();
            Read_Shared_Folder();
            Read_Current_Session(); 
        }




        private void settingsToolStripMenuItem1_Click_1(object sender, EventArgs e)
        {
            if (this.settingsToolStripMenuItem1.Checked == true)
            {
                _ShowPopUp = false;
                this.settingsToolStripMenuItem1.Checked = false;
            }
            else
            {
                _ShowPopUp = true;
                this.settingsToolStripMenuItem1.Checked = true;
            }

            //MessageBox.Show(_ShowPopUp.ToString());
            
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            toolStripMenuItem6.Checked = true;
            toolStripMenuItem7.Checked = false ;
            toolStripMenuItem8.Checked = false;
            toolStripMenuItem9.Checked = false;
            toolStripMenuItem10.Checked = false;
            _RefreshTime = 5;

        }

        private void toolStripMenuItem7_Click(object sender, EventArgs e)
        {
            toolStripMenuItem6.Checked = false;
            toolStripMenuItem7.Checked = true;
            toolStripMenuItem8.Checked = false;
            toolStripMenuItem9.Checked = false;
            toolStripMenuItem10.Checked = false;
            _RefreshTime = 10;
        }

        private void toolStripMenuItem8_Click(object sender, EventArgs e)
        {
            toolStripMenuItem6.Checked = false;
            toolStripMenuItem7.Checked = false;
            toolStripMenuItem8.Checked = true;
            toolStripMenuItem9.Checked = false;
            toolStripMenuItem10.Checked = false;
            _RefreshTime = 25;
        }

        private void toolStripMenuItem9_Click(object sender, EventArgs e)
        {
            toolStripMenuItem6.Checked = false ;
            toolStripMenuItem7.Checked = false;
            toolStripMenuItem8.Checked = false;
            toolStripMenuItem9.Checked = true;
            toolStripMenuItem10.Checked = false;
            _RefreshTime = 30;
        }

        private void toolStripMenuItem10_Click(object sender, EventArgs e)
        {
            toolStripMenuItem6.Checked = false;
            toolStripMenuItem7.Checked = false;
            toolStripMenuItem8.Checked = false;
            toolStripMenuItem9.Checked = false;
            toolStripMenuItem10.Checked = true;
            _RefreshTime = 50;
        }

        private void aboutMeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About abt = new About();
            abt.ShowDialog();
        }

        private void helpToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("VerySimple To Use", "SpyNet", MessageBoxButtons.OK, MessageBoxIcon.Information); 

        }


        /// <summary>
        /// Click on Clear Log History
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Right_ClearLogHistry_Click(object sender, EventArgs e)
        {
            switch (_CurrentActiveTab)
            {
                case 1:
                    ClearLogHistory(1);
                    break;
                case 2:
                    ClearLogHistory(2);
                    break;
                case 3:
                    ClearLogHistory(3);
                    break;
                case 4:
                    ClearLogHistory(4);
                    break;
            }
            
        }
        /// <summary>
        /// Clearing The History of Current Grid
        /// </summary>
        /// <param name="_ActiveTab">Current Selected Grid</param>
        protected void ClearLogHistory(int _ActiveTab)
        {
            int Count;
            switch (_ActiveTab)
            {
                case 1: //Clear The Create Grid List
                     Count = lstCreate.Items.Count;
                     while (Count != 0)
                     {
                         Count--;
                         lstCreate.Items[Count].Remove();
                         
                     }
                     cCount = 0;
                    break;
                case 2:  //Clear The Delete Grid List
                     Count = lstDelete.Items.Count;                   for (int i = 0; i < Count; i++)
                         while (Count != 0)
                         {
                             Count--;
                             lstDelete.Items[Count].Remove();

                         }
                     dCount = 0;
                    break;
                case 3:   //Clear The Renamed Grid List
                     Count = lstRenamed.Items.Count;
                     while (Count != 0)
                     {
                         Count--;
                         lstRenamed.Items[Count].Remove();

                     }
                     rCount = 0;
                    break;
                case 4: //Clear The Changed Grid List
                     Count = lstChanged.Items.Count;
                     while (Count != 0)
                     {
                         Count--;
                         lstChanged.Items[Count].Remove();
                      
                      }
                      cHCount = 0;
                    break;
            }
        }

        /// <summary>
        /// Check The Right Click on Create File List Grid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lstCreate_MouseDown(object sender, MouseEventArgs e)
        {
            if(e.Button==MouseButtons.Right)
                _CurrentActiveTab = 1;
        }
        /// <summary>
        /// Check Right Click on File Delete Grid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lstDelete_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
                _CurrentActiveTab = 2;
        }
/// <summary>
/// Check Right Click on Rename Grid
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
        private void lstRenamed_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
                _CurrentActiveTab = 3;
        }
        /// <summary>
        /// Check Right Click on File Changed Grid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void lstChanged_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
                _CurrentActiveTab = 4;
        }

      /// <summary>
      /// Right Click for check the current Active/ Accessing USer
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
        private void RightCurrentUser_Click(object sender, EventArgs e)
        {
            int Count;
            string _ActiveUser="Current Active IP :\n";
             Count = lviewSession.Items.Count;
                     while (Count != 0)
                     {
                         Count--;

                        _ActiveUser+= lviewSession.Items[Count].Text.ToString() + "\n";   
                     }
                     MessageBox.Show(_ActiveUser, "SpyNet : Your Network Spy", MessageBoxButtons.OK, MessageBoxIcon.Information); 
                             
        }
        /// <summary>
        /// Export Result To Text File !!!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RightExportToFile_Click(object sender, EventArgs e)
        {
            switch (_CurrentActiveTab)
            {
                case 1:
                    ExportToTextFile(1);
                    break;
                case 2:
                    ExportToTextFile(2);
                    break;
                case 3:
                    ExportToTextFile(3);
                    break;
                case 4:
                    ExportToTextFile(4);
                    break;

            }
            
        }
        /// <summary>
        /// Export The ListView Result To TextFile
        /// </summary>
        /// <param name="_ActiveTab">Tab Index</param>
        public void ExportToTextFile(int _ActiveTab)
        {

            int Count;
            string _Result="";
            bool _ClearFlag = false;
            switch(_ActiveTab)
            {
                case 1:
                 Count=lstCreate.Items.Count;
                 if (Count != 0)
                 {
                     TextWriter Tw = new StreamWriter(_CreateLogFilePath);
                     Tw.WriteLine("Created FilePath" + "\t" + "Date" + "\t" + "Time");
                     Tw.WriteLine("-----------------------------------------------------------------");
                     for (int j = 0; j < Count; j++)
                     {
                         ListViewItem lvi = lstCreate.Items[j];

                         for (int i = 0; i < lvi.SubItems.Count; i++)
                         {

                             _Result += lvi.SubItems[i].Text + "\t";
                         }

                         Tw.WriteLine(_Result);
                         _Result = "";
                     }
                     Tw.Close();
                     _Result = "";
                     _ClearFlag = true;

                 }
                 else
                 {
                     MessageBox.Show("Nothing to store in log file !", "SpyNet: Logging", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                     _ClearFlag = false;
                 }
                 break;

             case 2:
                 Count = lstDelete.Items.Count;
                 if (Count != 0)
                 {
                     TextWriter Tw1 = new StreamWriter(_DeleteLogFilePath);
                     Tw1.WriteLine("Deleted FilePath" + "\t" + "Date" + "\t" + "Time");
                     Tw1.WriteLine("-----------------------------------------------------------------");
                     for (int j = 0; j < Count; j++)
                     {
                         ListViewItem lvi = lstDelete.Items[j];
                         for (int i = 0; i < lvi.SubItems.Count; i++)
                         {
                             _Result += lvi.SubItems[i].Text + "\t";
                         }
                         Tw1.WriteLine(_Result);
                         _Result = "";
                     }
                     Tw1.Close();
                     _Result = "";
                     _ClearFlag = true;
                 }
                 else
                 {
                     MessageBox.Show("Nothing to store in log file !", "SpyNet: Logging", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                     _ClearFlag = false;
                 }
                 break;
             case 3:
                 Count = lstRenamed.Items.Count;
                 if (Count != 0)
                 {
                     TextWriter Tw2 = new StreamWriter(_RenamedLogFilePath);
                     Tw2.WriteLine("Old FilePath" + "\t" + "New File Path" + "\t" + "Date" + "\t" + "Time");
                     Tw2.WriteLine("-----------------------------------------------------------------");
                     for (int j = 0; j < Count; j++)
                     {
                         ListViewItem lvi = lstRenamed.Items[j];
                         for (int i = 0; i < lvi.SubItems.Count; i++)
                         {
                             _Result += lvi.SubItems[i].Text + "\t";
                         }
                         Tw2.WriteLine(_Result);
                         _Result = "";
                     }
                     Tw2.Close();
                     _Result = "";
                     _ClearFlag = true;
                 }
                 else
                 {
                     MessageBox.Show("Nothing to store in log file !", "SpyNet: Logging", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                     _ClearFlag = false;
                 }
                 break;
             case 4:
                 Count = lstChanged.Items.Count;
                 if (Count != 0)
                 {
                     TextWriter Tw3 = new StreamWriter(_ChangedLogFilePath);
                     Tw3.WriteLine("Changed FilePath" + "\t" + "Date" + "\t" + "Time");
                     Tw3.WriteLine("-----------------------------------------------------------------");
                     for (int j = 0; j < Count; j++)
                     {
                         ListViewItem lvi = lstChanged.Items[j];
                         for (int i = 0; i < lvi.SubItems.Count; i++)
                         {
                             _Result += lvi.SubItems[i].Text + "\t";
                         }
                         Tw3.WriteLine(_Result);
                         _Result = "";
                     }
                     Tw3.Close();
                     _Result = "";
                     _ClearFlag = true;
                 }
                 else
                 {
                     MessageBox.Show("Nothing to store in log file !", "SpyNet: Logging", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                     _ClearFlag = false;
                 }
                 break;
            }

            if (_ClearFlag == true)
            {
                DialogResult Result = MessageBox.Show("Log has been written successfuly !\n Do you want to clear this Grid ?", "SpyNet : Loging", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                if (Result == DialogResult.Yes)
                {
                    //Clear the Grid 
                    ClearLogHistory(_CurrentActiveTab);
                }
            }
            
        }

        }




    
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India
.NET Consultant | Former Microsoft MVP - ASP.NET | CodeProject MVP, Mentor, Insiders| Technology Evangelist | Author | Speaker | Geek | Blogger | Husband

Blog : http://abhijitjana.net
Web Site : http://dailydotnettips.com
Twitter : @AbhijitJana
My Kinect Book : Kinect for Windows SDK Programming Guide

Comments and Discussions