static void TestClassMkLink() { WriteLine("---- Test MkLink Class ----"); MkLink.Execute("A-D", "A", MkLinkParameter.Directory).PrintMkLinkResult(); MkLink.Execute("P-S", "PlayGround.exe", MkLinkParameter.SymbolicLink).PrintMkLinkResult(); MkLink.Execute("P-SH", "PlayGround.exe", MkLinkParameter.HardLink).PrintMkLinkResult(); MkLink.Execute("A-J", "A", MkLinkParameter.Junction).PrintMkLinkResult(); MkLink.Execute("A-JD", "A", MkLinkParameter.Junction | MkLinkParameter.Directory).PrintMkLinkResult(); MkLink.Execute("A-JH", "A", MkLinkParameter.Junction | MkLinkParameter.HardLink).PrintMkLinkResult(); MkLink.Execute("A-JHD", "A", MkLinkParameter.Junction | MkLinkParameter.HardLink | MkLinkParameter.Directory).PrintMkLinkResult(); MkLink.Execute("A-HD", "A", MkLinkParameter.Directory | MkLinkParameter.HardLink).PrintMkLinkResult(); }
/// <summary> /// /// </summary> /// <exception cref="LinkHasBeenExistedException"></exception> /// <exception cref="DifferentFromDriveLetterException"></exception> /// <exception cref="HardLinkIsInapplicableExcption"></exception> /// <exception cref="TargetNeitherFileNorDirectoryExcption"></exception> /// <exception cref="NotSelectLinkModeException"></exception> /// <exception cref="LinkDirectroyIsNotExistedException"></exception> /// <exception cref="LinkNameIsInvalidException"></exception> /// <exception cref="CancelOperationException"></exception> /// <exception cref="DirectorySymbolicLinkIsInapplicableExcption"></exception> /// <exception cref="FileSymbolicLinkIsInapplicableExcption"></exception> /// <exception cref="LinkDirectoryNameIsInvalidException"></exception> void CreateFunc() { if (!IsValid(LinkName.Text)) { throw new LinkNameIsInvalidException(); } if (!IsValid(LinkDirectoryName.Text)) { throw new LinkDirectoryNameIsInvalidException(); } var linkFullName = $@"{LinkDirectoryName.Text}\{LinkName.Text}"; var linkDir = new DirectoryInfo(LinkDirectoryName.Text); var ifLinkIsDir = new DirectoryInfo(linkFullName); var ifLinkFile = new FileInfo(linkFullName); var ifTargetFile = new FileInfo(TargetPath.Text); var ifTargetDir = new DirectoryInfo(TargetPath.Text); if (ifLinkIsDir.Exists || ifLinkFile.Exists)//当所要创建的链接已经存在的时候 { throw new LinkHasBeenExistedException(); } if (!linkDir.Exists)//所要创建的链接,它所在的文件夹不存在时 { throw new LinkDirectroyIsNotExistedException(LinkDirectoryName.Text); } LinkMode link; //当选择Hard Link时,此时需要两者都是文件 if (HardLinkButton.IsChecked == true) { //硬链接要求目标文件存在 if (ifTargetFile.Exists) { if (GetDriveLetter(ifTargetFile.FullName) == GetDriveLetter(ifLinkFile.FullName)) { link = LinkMode.H; string TargetFileExtension = ifTargetFile.Extension; if (TargetFileExtension != ifLinkFile.Extension) { var res = MessageBox.Show(string.Format(Properties.Resources.ExtensionIsNotSame, TargetFileExtension), Properties.Resources.Tip, MessageBoxButton.OKCancel); if (res == MessageBoxResult.OK) { LinkName.Text += $"{TargetFileExtension}"; linkFullName = $@"{LinkDirectoryName.Text}\{LinkName.Text}"; } } } else { throw new DifferentFromDriveLetterException(); } } else { throw new HardLinkIsInapplicableException(TargetPath.Text); } } //当选择Junction Link时 else if (JunctionLinkButton.IsChecked == true) { //判断目标地址是否是文件夹,此时需要两者都是目录 if (!ifTargetDir.Exists) { var res = MessageBox.Show($"{Properties.Resources.TargetHasNotExistedYet}\n{Properties.Resources.WhetherMkDirTarget}", Properties.Resources.Tip, MessageBoxButton.OKCancel); if (res == MessageBoxResult.OK) { ifTargetDir.Create(); } } link = LinkMode.J; } //当选择Directory Symbolic Link时 else if (DirectorySymbolicLinkButton.IsChecked == true) { if (ifTargetFile.Exists) { throw new DirectorySymbolicLinkIsInapplicableException(); } //当目标地址是文件夹时 if (!ifTargetDir.Exists) { var res = MessageBox.Show($"{Properties.Resources.TargetHasNotExistedYet}\n{Properties.Resources.WhetherMkDirTarget}", Properties.Resources.Tip, MessageBoxButton.OKCancel); if (res == MessageBoxResult.OK) { ifTargetDir.Create(); } } link = LinkMode.Ddir; } //当选择File Symbolic Link时 else if (FileSymbolicLinkButton.IsChecked == true) { if (ifTargetDir.Exists) { throw new FileSymbolicLinkIsInapplicableException(); } if (!ifTargetFile.Exists) { var res = MessageBox.Show($"{Properties.Resources.TargetHasNotExistedYet}\n{Properties.Resources.WhetherContinue}", Properties.Resources.Tip, MessageBoxButton.OKCancel); if (res != MessageBoxResult.OK) { throw new CancelOperationException(); } } link = LinkMode.Dfile; } //此时没有选择Mode else { throw new NotSelectLinkModeException(); } //运行CMD var cmd = new MkLink(link, linkFullName, TargetPath.Text); cmd.Run(); }