Click here to Skip to main content
15,891,184 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.Windows.Forms;
using System.Collections;
using System.IO;
using System.Diagnostics;

using Common;

namespace HardLinks
{
	/// <summary>
	/// Summary description for Methods.
	/// </summary>
	public class Methods
	{
		public static Links ScanDisk( string RealRoot, string LinkRoot )
		{
			Links Links = new Links();

			Links.AddDirectory( RealRoot );
			Links.AddDirectory( RealRoot, LinkRoot );

			DirectoryInfo di = new DirectoryInfo( RealRoot );
			if ( ! di.Exists ) { Debug.Assert( true ); return Links; }

			FileStream fsRealFile = null;
			FileStream fsLinkFile = null;
			FileInfo[] Files = di.GetFiles();
			foreach ( FileInfo RealFile in Files )
			{
				BY_HANDLE_FILE_INFORMATION infoRealFile;
				fsRealFile = RealFile.Open( FileMode.Open, FileAccess.Read, FileShare.ReadWrite );
				bool bRealFile = Kernel32.GetFileInformationByHandle( fsRealFile.Handle, out infoRealFile );
				if ( ! bRealFile ) { Debug.Assert( false ); goto next; }

				string LinkFileName = LinkRoot + "\\" + RealFile.Name;
				FileInfo LinkFile = new FileInfo( LinkFileName );

				if ( ! LinkFile.Exists )
				{
					Links.Add( RealRoot, LinkRoot, RealFile.Name, Linked.NoFile );
					goto next;
				}

				BY_HANDLE_FILE_INFORMATION infoLinkFile;
				fsLinkFile = LinkFile.Open( FileMode.Open, FileAccess.Read, FileShare.ReadWrite );
				bool bLinkFile = Kernel32.GetFileInformationByHandle( fsLinkFile.Handle, out infoLinkFile );
				if ( ! bLinkFile ) { Debug.Assert( false ); goto next; }

				if ( infoLinkFile.dwVolumeSerialNumber != infoRealFile.dwVolumeSerialNumber ||
					infoLinkFile.nFileIndexHigh != infoRealFile.nFileIndexHigh ||
					infoLinkFile.nFileIndexLow  != infoRealFile.nFileIndexLow  )
				{
					string s = "Link file exists, but is not a link to the real file !\n\n"
						+ "Real file : " + RealFile.FullName + "\n"
						+ "Link file : " + LinkFile.FullName ;
//					MessageBox.Show( s, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning );
					Links.Add( RealRoot, LinkRoot, RealFile.Name, Linked.FileExists );
					goto next;
				}

				Links.Add( RealRoot, LinkRoot, RealFile.Name, Linked.Linked );

			next:
				if ( fsLinkFile != null ) fsLinkFile.Close();
				if ( fsRealFile != null ) fsRealFile.Close();
			}

			return Links;
		}
	}
}

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