Click here to Skip to main content
15,895,871 members
Articles / Programming Languages / C#

HardLinks - Manage your library of common classes

Rate me:
Please Sign up or sign in to vote.
4.55/5 (4 votes)
5 Mar 2004CPOL3 min read 59.7K   1.4K   30  
A tool for managing NTFS hard links
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Diagnostics;

using Common;

using Crownwood.Magic.Common;
using Crownwood.Magic.Docking;

namespace HardLinks
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class FormMain : System.Windows.Forms.Form
	{
		private Links Links = new Links();

		private bool Loading = false;

		private string SelectedRealDirectory = "";
		private string SelectedLinkDirectory = "";
		private string SelectedFilename      = "";

		private DockingManager dm = null;

		private ListBox ListBoxRealDirectory = null;
		private ListBox ListBoxLinkDirectory = null;
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.CheckedListBox CheckedListBoxFiles;
		private System.Windows.Forms.ImageList ImageList;
		private System.Windows.Forms.ToolBar ToolBar;
		private System.Windows.Forms.ToolBarButton ToolBarButtonFileOpen;
		private System.Windows.Forms.ToolBarButton ToolBarButtonFileSave;
		private System.Windows.Forms.ToolBarButton ToolBarButtonSeparator1;
		private System.Windows.Forms.ToolBarButton ToolBarButtonFileNew;
		private System.Windows.Forms.ToolBarButton ToolBarButtonViewRefresh;
		private System.Windows.Forms.ToolBarButton ToolBarButtonSeparator2;
		private System.Windows.Forms.Timer Timer;
		private System.Windows.Forms.ToolBarButton ToolBarButtonHelpAbout;
		private System.Windows.Forms.ToolBarButton ToolBarButtonWinDiff;
		private System.Windows.Forms.ToolBarButton ToolBarButtonSeparator3;
		private System.Windows.Forms.ToolBarButton ToolBarButtonSeparator4;
		private System.Windows.Forms.ToolBarButton ToolBarButtonOptions;
		private System.Windows.Forms.ToolBarButton ToolBarButtonSeparator5;
		private System.Windows.Forms.ToolBarButton ToolBarButtonEditDelete;

		public FormMain()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			SelectedRealDirectory = Settings.Get( "SelectedRealDirectory" , "" );
			SelectedLinkDirectory = Settings.Get( "SelectedLinkDirectory" , "" );
			SelectedFilename      = Settings.Get( "SelectedFilename"      , "" );

		
			ListBoxRealDirectory = new ListBox();
			ListBoxLinkDirectory = new ListBox();

			ContextMenu ContextMenuRealDirectory = new ContextMenu();
			ContextMenuRealDirectory.MenuItems.Add( new MenuItem( "Add...", new EventHandler( OnAddRealDirectory ) ) );
			ContextMenuRealDirectory.MenuItems.Add( new MenuItem( "Remove", new EventHandler( OnRemoveRealDirectory ) ) );
			ListBoxRealDirectory.ContextMenu = ContextMenuRealDirectory;

			ContextMenu ContextMenuLinkDirectory = new ContextMenu();
			ContextMenuLinkDirectory.MenuItems.Add( new MenuItem( "Add...", new EventHandler( OnAddLinkDirectory ) ) );
			ContextMenuLinkDirectory.MenuItems.Add( new MenuItem( "Remove", new EventHandler( OnRemoveLinkDirectory ) ) );
			ListBoxLinkDirectory.ContextMenu = ContextMenuLinkDirectory;

			ContextMenu ContextMenuFiles = new ContextMenu();
			ContextMenuFiles.MenuItems.Add( new MenuItem( "Legend...", new EventHandler( OnFilesLegend ) ) );
			CheckedListBoxFiles.ContextMenu = ContextMenuFiles;

			dm = new DockingManager( this, VisualStyle.IDE );

			dm.InnerControl = CheckedListBoxFiles;
			dm.OuterControl = ToolBar;

			Content ContentRealDirectory = dm.Contents.Add( ListBoxRealDirectory, "Real Directories" );
			Content ContentLinkDirectory = dm.Contents.Add( ListBoxLinkDirectory, "Link Directories" );

			WindowContent wc = dm.AddContentWithState( ContentRealDirectory, State.DockLeft );
			Window w = dm.AddContentToZone( ContentLinkDirectory, wc.ParentZone, 1 );

			dm.ResizeBarVector = 7;
			wc.ParentZone.Width = 400;

			dm.ShowAllContents();

			ListBoxRealDirectory.Sorted = true;
			ListBoxLinkDirectory.Sorted = true;

			ListBoxRealDirectory.SelectedIndexChanged += new EventHandler( ListBoxRealDirectory_SelectedIndexChanged );
			ListBoxLinkDirectory.SelectedIndexChanged += new EventHandler( ListBoxLinkDirectory_SelectedIndexChanged );

			Timer.Enabled = true;
		}

		private void Timer_Tick(object sender, System.EventArgs e)
		{
			Timer.Enabled = false;
		
			OnFileNew();
		}

		private void FormMain_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if ( ListBoxRealDirectory.SelectedIndex >= 0 ) Settings.Set( "SelectedRealDirectory" , ListBoxRealDirectory.SelectedItem.ToString() );
			if ( ListBoxLinkDirectory.SelectedIndex >= 0 ) Settings.Set( "SelectedLinkDirectory" , ListBoxLinkDirectory.SelectedItem.ToString() );
			if ( CheckedListBoxFiles .SelectedIndex >= 0 ) Settings.Set( "SelectedFilename"      , CheckedListBoxFiles  .Text );
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FormMain));
			this.CheckedListBoxFiles = new System.Windows.Forms.CheckedListBox();
			this.ImageList = new System.Windows.Forms.ImageList(this.components);
			this.ToolBar = new System.Windows.Forms.ToolBar();
			this.ToolBarButtonFileNew = new System.Windows.Forms.ToolBarButton();
			this.ToolBarButtonFileSave = new System.Windows.Forms.ToolBarButton();
			this.ToolBarButtonFileOpen = new System.Windows.Forms.ToolBarButton();
			this.ToolBarButtonEditDelete = new System.Windows.Forms.ToolBarButton();
			this.ToolBarButtonSeparator1 = new System.Windows.Forms.ToolBarButton();
			this.ToolBarButtonViewRefresh = new System.Windows.Forms.ToolBarButton();
			this.ToolBarButtonSeparator2 = new System.Windows.Forms.ToolBarButton();
			this.ToolBarButtonWinDiff = new System.Windows.Forms.ToolBarButton();
			this.ToolBarButtonSeparator3 = new System.Windows.Forms.ToolBarButton();
			this.ToolBarButtonOptions = new System.Windows.Forms.ToolBarButton();
			this.ToolBarButtonSeparator4 = new System.Windows.Forms.ToolBarButton();
			this.ToolBarButtonHelpAbout = new System.Windows.Forms.ToolBarButton();
			this.ToolBarButtonSeparator5 = new System.Windows.Forms.ToolBarButton();
			this.Timer = new System.Windows.Forms.Timer(this.components);
			this.SuspendLayout();
			// 
			// CheckedListBoxFiles
			// 
			this.CheckedListBoxFiles.CheckOnClick = true;
			this.CheckedListBoxFiles.Dock = System.Windows.Forms.DockStyle.Fill;
			this.CheckedListBoxFiles.Location = new System.Drawing.Point(0, 26);
			this.CheckedListBoxFiles.Name = "CheckedListBoxFiles";
			this.CheckedListBoxFiles.Size = new System.Drawing.Size(632, 319);
			this.CheckedListBoxFiles.Sorted = true;
			this.CheckedListBoxFiles.TabIndex = 6;
			this.CheckedListBoxFiles.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.CheckedListBoxFiles_ItemCheck);
			// 
			// ImageList
			// 
			this.ImageList.ImageSize = new System.Drawing.Size(16, 16);
			this.ImageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("ImageList.ImageStream")));
			this.ImageList.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// ToolBar
			// 
			this.ToolBar.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
			this.ToolBar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
																					   this.ToolBarButtonFileNew,
																					   this.ToolBarButtonFileSave,
																					   this.ToolBarButtonFileOpen,
																					   this.ToolBarButtonEditDelete,
																					   this.ToolBarButtonSeparator1,
																					   this.ToolBarButtonViewRefresh,
																					   this.ToolBarButtonSeparator2,
																					   this.ToolBarButtonWinDiff,
																					   this.ToolBarButtonSeparator3,
																					   this.ToolBarButtonOptions,
																					   this.ToolBarButtonSeparator4,
																					   this.ToolBarButtonHelpAbout,
																					   this.ToolBarButtonSeparator5});
			this.ToolBar.ButtonSize = new System.Drawing.Size(23, 22);
			this.ToolBar.Divider = false;
			this.ToolBar.DropDownArrows = true;
			this.ToolBar.ImageList = this.ImageList;
			this.ToolBar.Location = new System.Drawing.Point(0, 0);
			this.ToolBar.Name = "ToolBar";
			this.ToolBar.ShowToolTips = true;
			this.ToolBar.Size = new System.Drawing.Size(632, 26);
			this.ToolBar.TabIndex = 0;
			this.ToolBar.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.ToolBar_ButtonClick);
			// 
			// ToolBarButtonFileNew
			// 
			this.ToolBarButtonFileNew.ImageIndex = 0;
			this.ToolBarButtonFileNew.ToolTipText = "Load from registry";
			this.ToolBarButtonFileNew.Visible = false;
			// 
			// ToolBarButtonFileSave
			// 
			this.ToolBarButtonFileSave.ImageIndex = 1;
			this.ToolBarButtonFileSave.ToolTipText = "Save to registry";
			this.ToolBarButtonFileSave.Visible = false;
			// 
			// ToolBarButtonFileOpen
			// 
			this.ToolBarButtonFileOpen.ImageIndex = 2;
			this.ToolBarButtonFileOpen.ToolTipText = "Add directory";
			// 
			// ToolBarButtonEditDelete
			// 
			this.ToolBarButtonEditDelete.ImageIndex = 3;
			this.ToolBarButtonEditDelete.ToolTipText = "Remove directory";
			// 
			// ToolBarButtonSeparator1
			// 
			this.ToolBarButtonSeparator1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// ToolBarButtonViewRefresh
			// 
			this.ToolBarButtonViewRefresh.ImageIndex = 4;
			this.ToolBarButtonViewRefresh.ToolTipText = "Refresh";
			// 
			// ToolBarButtonSeparator2
			// 
			this.ToolBarButtonSeparator2.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// ToolBarButtonWinDiff
			// 
			this.ToolBarButtonWinDiff.ImageIndex = 5;
			this.ToolBarButtonWinDiff.ToolTipText = "WinDiff";
			// 
			// ToolBarButtonSeparator3
			// 
			this.ToolBarButtonSeparator3.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// ToolBarButtonOptions
			// 
			this.ToolBarButtonOptions.ImageIndex = 6;
			this.ToolBarButtonOptions.ToolTipText = "Options";
			// 
			// ToolBarButtonSeparator4
			// 
			this.ToolBarButtonSeparator4.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// ToolBarButtonHelpAbout
			// 
			this.ToolBarButtonHelpAbout.ImageIndex = 7;
			this.ToolBarButtonHelpAbout.ToolTipText = "About";
			// 
			// ToolBarButtonSeparator5
			// 
			this.ToolBarButtonSeparator5.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// Timer
			// 
			this.Timer.Tick += new System.EventHandler(this.Timer_Tick);
			// 
			// FormMain
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(632, 354);
			this.Controls.Add(this.CheckedListBoxFiles);
			this.Controls.Add(this.ToolBar);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "FormMain";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "HardLinks";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.FormMain_Closing);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run( new FormMain() );
		}

//-----------------------------------------------------------------------------

		private void ToolBar_ButtonClick( object sender, ToolBarButtonClickEventArgs e )
		{
			if ( e.Button == ToolBarButtonFileNew     ) OnFileNew     ();
			if ( e.Button == ToolBarButtonFileSave    ) OnFileSave    ();
			if ( e.Button == ToolBarButtonFileOpen    ) OnFileOpen    ();
			if ( e.Button == ToolBarButtonEditDelete  ) OnEditDelete  ();
			if ( e.Button == ToolBarButtonViewRefresh ) OnViewRefresh ();
			if ( e.Button == ToolBarButtonWinDiff     ) OnWinDiff     ();
			if ( e.Button == ToolBarButtonOptions     ) OnOptions     ();
			if ( e.Button == ToolBarButtonHelpAbout   ) OnHelpAbout   ();
		}

		private void OnAddRealDirectory( object sender, EventArgs e )
		{
			DlgScanDiskOptions dlg = new DlgScanDiskOptions();
			dlg.Text = "Add directory";

			RealDirectory RealDirectory = (RealDirectory) ListBoxRealDirectory.SelectedItem;
			if ( RealDirectory != null ) dlg.RealDirectory = RealDirectory.RealDirectoryName;

//			LinkDirectory LinkDirectory = (LinkDirectory) ListBoxLinkDirectory.SelectedItem;
//			if ( LinkDirectory != null ) dlg.LinkDirectory = LinkDirectory.LinkDirectoryName;

			if ( dlg.ShowDialog() != DialogResult.OK ) return;

			AddDirectories( dlg.RealDirectory, dlg.LinkDirectory );
		}

		private void OnAddLinkDirectory( object sender, EventArgs e )
		{
			DlgScanDiskOptions dlg = new DlgScanDiskOptions();
			dlg.Text = "Add directory";

			RealDirectory RealDirectory = (RealDirectory) ListBoxRealDirectory.SelectedItem;
			if ( RealDirectory != null ) dlg.RealDirectory = RealDirectory.RealDirectoryName;

			LinkDirectory LinkDirectory = (LinkDirectory) ListBoxLinkDirectory.SelectedItem;
			if ( LinkDirectory != null ) dlg.LinkDirectory = LinkDirectory.LinkDirectoryName;

			if ( dlg.ShowDialog() != DialogResult.OK ) return;

			AddDirectories( dlg.RealDirectory, dlg.LinkDirectory );
		}

		private void OnRemoveRealDirectory( object sender, EventArgs e )
		{
			RealDirectory RealDirectory = (RealDirectory) ListBoxRealDirectory.SelectedItem;
			if ( RealDirectory == null ) return;

			Links.RemoveDirectory( RealDirectory.RealDirectoryName, "" );

			Links.SaveToRegistry();
			UpdateFromLinks();
		}

		private void OnRemoveLinkDirectory( object sender, EventArgs e )
		{
			RealDirectory RealDirectory = (RealDirectory) ListBoxRealDirectory.SelectedItem;
			if ( RealDirectory == null ) return;

			LinkDirectory LinkDirectory = (LinkDirectory) ListBoxLinkDirectory.SelectedItem;
			if ( LinkDirectory == null ) return;

			Links.RemoveDirectory( RealDirectory.RealDirectoryName, LinkDirectory.LinkDirectoryName );

			Links.SaveToRegistry();
			UpdateFromLinks();
		}

		private void OnFilesLegend( object sender, EventArgs e )
		{
			MessageBox.Show(
				"Unchecked:\t No File\n" + 
				"Indeterminate:\t File Exists\n" +
				"Checked:\t\t Link Exists    ",
				"HardLinks" );
		}

//-----------------------------------------------------------------------------

		private void OnFileNew()
		{
			Links.LoadFromRegistry();
			Links.ScanDisk();
			UpdateFromLinks();
		}

		private void OnFileOpen()
		{
			DlgScanDiskOptions dlg = new DlgScanDiskOptions();
			dlg.Text = "Add directory";

			RealDirectory RealDirectory = (RealDirectory) ListBoxRealDirectory.SelectedItem;
			if ( RealDirectory != null ) dlg.RealDirectory = RealDirectory.RealDirectoryName;

			LinkDirectory LinkDirectory = (LinkDirectory) ListBoxLinkDirectory.SelectedItem;
			if ( LinkDirectory != null ) dlg.LinkDirectory = LinkDirectory.LinkDirectoryName;

			if ( dlg.ShowDialog() != DialogResult.OK ) return;

			AddDirectories( dlg.RealDirectory, dlg.LinkDirectory );
		}

		private void OnFileSave()
		{
			Links.SaveToRegistry();
		}

		private void OnEditDelete()
		{
			DlgScanDiskOptions dlg = new DlgScanDiskOptions();
			dlg.Text = "Remove directory";

			RealDirectory RealDirectory = (RealDirectory) ListBoxRealDirectory.SelectedItem;
			if ( RealDirectory != null ) dlg.RealDirectory = RealDirectory.RealDirectoryName;

			LinkDirectory LinkDirectory = (LinkDirectory) ListBoxLinkDirectory.SelectedItem;
			if ( LinkDirectory != null ) dlg.LinkDirectory = LinkDirectory.LinkDirectoryName;

			if ( dlg.ShowDialog() != DialogResult.OK ) return;

			Links.RemoveDirectory( dlg.RealDirectory, dlg.LinkDirectory );

			Links.SaveToRegistry();
			UpdateFromLinks();
		}

		private void OnViewRefresh()
		{
			Links.ScanDisk();
			UpdateFromLinks();
		}

		private void OnWinDiff()
		{
			if ( ListBoxRealDirectory.SelectedIndex < 0 ) return;
			if ( ListBoxLinkDirectory.SelectedIndex < 0 ) return;

			string RealDirectory = ListBoxRealDirectory.SelectedItem.ToString();
			string LinkDirectory = ListBoxLinkDirectory.SelectedItem.ToString();

			string WinDiff = Settings.Get( Global.WinDiff, Global.DefaultWinDiffPath );

			System.Diagnostics.Process.Start( WinDiff, "-O -D \"" + RealDirectory + "\" \"" + LinkDirectory + "\"" );
		}

		private void OnOptions()
		{
			string WinDiff = Settings.Get( Global.WinDiff, Global.DefaultWinDiffPath );

			DlgOptions dlg = new DlgOptions();
			dlg.WinDiff = WinDiff;
			if ( dlg.ShowDialog() != DialogResult.OK ) return;

			Settings.Set( Global.WinDiff, dlg.WinDiff );
		}

		private void OnHelpAbout()
		{
			DlgAbout dlg = new DlgAbout();
			dlg.ShowDialog();
		}

//-----------------------------------------------------------------------------

		private void SelectRealDirectory( string RealDirectory )
		{
			object oSelect = null;
			foreach ( object o in ListBoxRealDirectory.Items )
				if ( o.ToString() == RealDirectory )
					oSelect = o;

			ListBoxRealDirectory.SelectedItem = oSelect;
		}

		private void SelectLinkDirectory( string LinkDirectory )
		{
			object oSelect = null;
			foreach ( object o in ListBoxLinkDirectory.Items )
				if ( o.ToString() == LinkDirectory )
					oSelect = o;

			ListBoxLinkDirectory.SelectedItem = oSelect;
		}

		private void AddDirectories( string RealDirectory, string LinkDirectory )
		{
			if ( RealDirectory.Length == 0 ) return;

			if ( LinkDirectory.Length == 0 )
			{
				Links.AddDirectory( RealDirectory );
			}
			else
			{
				Links LinksToAdd = Methods.ScanDisk( RealDirectory, LinkDirectory );
				Links.Add( LinksToAdd );
			}

			Links.SaveToRegistry();
			UpdateFromLinks();

			SelectRealDirectory( RealDirectory );
			SelectLinkDirectory( LinkDirectory );
		}

//-----------------------------------------------------------------------------

		private void UpdateFromLinks()
		{
			//			if ( ComboBoxRealDirectory .SelectedIndex >= 0 ) SelectedRealDirectory = ComboBoxRealDirectory.Text ;
			//			if ( ComboBoxLinkDirectory .SelectedIndex >= 0 ) SelectedLinkDirectory = ComboBoxLinkDirectory.Text ;
			//			if ( CheckedListBoxFiles   .SelectedIndex >= 0 ) SelectedFilename      = CheckedListBoxFiles  .Text ;

			if ( ListBoxRealDirectory.SelectedIndex >= 0 ) SelectedRealDirectory = ListBoxRealDirectory.SelectedItem.ToString();
			if ( ListBoxLinkDirectory.SelectedIndex >= 0 ) SelectedLinkDirectory = ListBoxLinkDirectory.SelectedItem.ToString();
			if ( CheckedListBoxFiles .SelectedIndex >= 0 ) SelectedFilename = CheckedListBoxFiles.Text ;

			ListBoxRealDirectory.Items.Clear();
			ListBoxLinkDirectory.Items.Clear();
			CheckedListBoxFiles .Items.Clear();

			foreach ( RealDirectory RealDirectory in Links.RealDirectories.Values )
				ListBoxRealDirectory.Items.Add( RealDirectory );

			SelectRealDirectory( SelectedRealDirectory );
		}

		private void ListBoxRealDirectory_SelectedIndexChanged( object sender, System.EventArgs e )
		{
			if ( ListBoxLinkDirectory.SelectedIndex >= 0 ) SelectedLinkDirectory = ListBoxLinkDirectory.SelectedItem.ToString();
			if ( CheckedListBoxFiles .SelectedIndex >= 0 ) SelectedFilename = CheckedListBoxFiles.Text ;

			ListBoxLinkDirectory.Items.Clear();
			CheckedListBoxFiles .Items.Clear();

			RealDirectory RealDirectory = (RealDirectory) ListBoxRealDirectory.SelectedItem;

			foreach ( LinkDirectory LinkDirectory in RealDirectory.LinkDirectories.Values )
				ListBoxLinkDirectory.Items.Add( LinkDirectory );

			SelectLinkDirectory( SelectedLinkDirectory );
		}

		private void ListBoxLinkDirectory_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if ( CheckedListBoxFiles.SelectedIndex >= 0 ) SelectedFilename = CheckedListBoxFiles.Text ;
			
			CheckedListBoxFiles.Items.Clear();

			LinkDirectory LinkDirectory = (LinkDirectory) ListBoxLinkDirectory.SelectedItem;

			Loading = true;

			foreach ( File File in LinkDirectory.Files.Values )
			{
				CheckState cs = CheckState.Indeterminate;
				switch ( File.Linked )
				{
				case Linked.Linked     : cs = CheckState.Checked       ; break;
				case Linked.FileExists : cs = CheckState.Indeterminate ; break;
				case Linked.NoFile     : cs = CheckState.Unchecked     ; break;
				default: Debug.Assert( false ); break;
				}
				CheckedListBoxFiles.Items.Add( File, cs );
			}

			Loading = false;

			CheckedListBoxFiles.Text = SelectedFilename;
		}

//-----------------------------------------------------------------------------

		private void CheckedListBoxFiles_ItemCheck( object sender, System.Windows.Forms.ItemCheckEventArgs e )
		{
			if ( Loading ) return;

			if ( e.CurrentValue == CheckState.Indeterminate )
				e.NewValue = CheckState.Unchecked;

			File File = (File) CheckedListBoxFiles.Items[ e.Index ];

			string RealFile = File.RealDirectoryName + "\\" + File.FileName;
			string LinkFile = File.LinkDirectoryName + "\\" + File.FileName;

			if ( e.NewValue == CheckState.Checked )
			{
				if ( ! Kernel32.CreateHardLink( LinkFile, RealFile, IntPtr.Zero ) )
				{
					string s = "Failed to create link : \n\nReal file : " + RealFile + "\nLink file : " + LinkFile;
					MessageBox.Show( s, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning );
					e.NewValue = CheckState.Unchecked;
				}
				else
					File.Linked = Linked.Linked;
			}
			else
			{
				try
				{
					System.IO.File.Delete( LinkFile );
					File.Linked = Linked.NoFile;
				}
				catch
				{
					string s = "Failed to delete link : \n\nReal file : " + RealFile + "\nLink file : " + LinkFile;
					MessageBox.Show( s, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning );
					e.NewValue = CheckState.Checked;
				}
			}
		}


//-----------------------------------------------------------------------------

	}
}

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
United Kingdom United Kingdom
I discovered C# and .NET 1.0 Beta 1 in late 2000 and loved them immediately.
I have been writing software professionally in C# ever since

In real life, I have spent 3 years travelling abroad,
I have held a UK Private Pilots Licence for 20 years,
and I am a PADI Divemaster.

I now live near idyllic Bournemouth in England.

I can work 'virtually' anywhere!

Comments and Discussions