Exemple #1
0
 private static WarningException ValidateParameter(MkLinkParameter para, bool isFile)
 {
     if (para == (MkLinkParameter.Directory | MkLinkParameter.HardLink))
     {
         return(new WarningException("This parameter combination may fail."));
     }
     if (
         (para.HasFlag(MkLinkParameter.Directory) || para.HasFlag(MkLinkParameter.Junction)) &&
         isFile
         )
     {
         return(new WarningException("The link made probably doesn't work.Don't make directory/junction link for files."));
     }
     if (!isFile &&
         (
             !para.HasFlag(MkLinkParameter.Directory)
             &&
             !para.HasFlag(MkLinkParameter.Junction)
         )
         )
     {
         return(new WarningException("The link made probably doesn't work.You should make directory/junction link for directories."));
     }
     return(null);
 }
Exemple #2
0
 static void PrintEnumInfo(MkLinkParameter para)
 {
     Write("name:");
     WriteLine(para);
     Write("value:");
     WriteLine((int)(para));
 }
Exemple #3
0
 public static string GetStringForm_Single(this MkLinkParameter para)
 {
     return(para switch {
         MkLinkParameter.Directory => "/D",
         MkLinkParameter.HardLink => "/H",
         MkLinkParameter.Junction => "/J",
         MkLinkParameter.SymbolicLink => "",
         _ => throw new ArgumentException("`para` can't be combinations", nameof(para))
     });
Exemple #4
0
        private static string GenerateArguments(string link, string target, MkLinkParameter para)
        {
            string parameter = para.GetStringForm();

            return($"/c mklink {parameter} {link} {target}");
        }
Exemple #5
0
        public static MkLinkResult Execute(string link, string target, MkLinkParameter para, bool overwriteExistedFile = false)
        {
            //throw new NotImplementedException();
            var lnkV = ValidateLink(link, overwriteExistedFile);

            if (lnkV.Item1 != null)
            {
                return(new MkLinkResult()
                {
                    IsSuccessful = false, Exception = lnkV.Item1, FailedStep = MkLinkFailedStep.ValidateLink
                });
            }
            var tarV = ValidateTarget(target);

            if (tarV.Item1 != null)
            {
                return(new MkLinkResult()
                {
                    IsSuccessful = false, Exception = tarV.Item1, FailedStep = MkLinkFailedStep.ValidateTarget
                });
            }
            var paVal = ValidateParameter(para, tarV.Item3);

            if (paVal != null)
            {
                return(new MkLinkResult()
                {
                    IsSuccessful = false, Exception = paVal, FailedStep = MkLinkFailedStep.ValidateParameter
                });
            }
            if (overwriteExistedFile)
            {
                if (File.Exists(link))
                {
                    try
                    {
                        File.Delete(link);
                    }
                    catch (Exception ex)
                    {
                        return(new MkLinkResult()
                        {
                            IsSuccessful = false, Exception = ex, FailedStep = MkLinkFailedStep.DeleteExistedFile
                        });
                    }
                }
                if (Directory.Exists(link))
                {
                    try
                    {
                        DirectoryInfo di = new DirectoryInfo(link);
                        foreach (var fi in di.GetFiles())
                        {
                            fi.Delete();
                        }
                        foreach (var dii in di.GetDirectories())
                        {
                            dii.Delete(true);
                        }
                        Directory.Delete(link);
                    }
                    catch (Exception ex)
                    {
                        return(new MkLinkResult()
                        {
                            IsSuccessful = false, Exception = ex, FailedStep = MkLinkFailedStep.DeleteExistedFile
                        });
                    }
                }
            }
            _mkLinkCommand.Arguments = GenerateArguments(lnkV.Item2, tarV.Item2, para);
            Process p;

            try
            {
                p = Process.Start(_mkLinkCommand);
                p.WaitForExit();
            }
            catch (Exception e)
            {
                return(new MkLinkResult()
                {
                    IsSuccessful = false, Exception = e, FailedStep = MkLinkFailedStep.StartMkLinkAndWaitForExit
                });
            }
            if (p.ExitCode == 0)
            {
                return new MkLinkResult()
                       {
                           IsSuccessful = true, Exception = null, FailedStep = MkLinkFailedStep.None
                       }
            }
            ;
            return(new MkLinkResult()
            {
                IsSuccessful = false, Exception = new Exception($"MkLink utility exited with code {p.ExitCode} (Not Zero)."), FailedStep = MkLinkFailedStep.CheckExitCode
            });
        }