Beispiel #1
0
        public void ContainsKey()
        {
            const string command_line = "file1.bin file2.bin -wps \"c:\\Program files 321\\123\" 123 "
                                        + "--in file3.txt file4.txt file5.txt --in file6.txt -o result.bin "
                                        + "-- file7.qwe file8.asd";

            var parser = new CommandLineArgs(command_line.Split(' '));

            Assert.That.Value(parser.ContainsKey("")).IsTrue();
            Assert.That.Value(parser.ContainsKey("123")).IsFalse();
            Assert.That.Value(parser.ContainsKey("w")).IsTrue();
            Assert.That.Value(parser.ContainsKey("p")).IsTrue();
            Assert.That.Value(parser.ContainsKey("s")).IsTrue();
            Assert.That.Value(parser.ContainsKey("in")).IsTrue();
            Assert.That.Value(parser.ContainsKey("o")).IsTrue();
            Assert.That.Value(parser.ContainsKey("--")).IsTrue();
        }
        static void Main()
        {
            var parameters = new CommandLineArgs(Environment.GetCommandLineArgs());

            if (!Debugger.IsAttached &&
                parameters.ContainsKey(DebugArgumentTitle) &&
                parameters[DebugArgumentTitle] == "True") {
                Debugger.Launch();
                Debugger.Break();
            }

            if (parameters.Count == 0) return;

            // Check privileges
            var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
            if (!principal.IsInRole(WindowsBuiltInRole.Administrator)) throw new UnauthorizedAccessException("Couldn't get administrator privileges");

            if (!parameters.ContainsKey(ActionArgumentTitle)) return;

            switch (parameters[ActionArgumentTitle]) {
                case ActionArgumentCreate: {
                    LinkType type;
                    if (!parameters.ContainsKey(CreateLinkPathArgumentTitle) ||
                        !parameters.ContainsKey(CreateDestinationArgumentTitle) ||
                        !parameters.ContainsKey(CreateLinkTypeArgumentTitle) ||
                        !Enum.TryParse(parameters[CreateLinkTypeArgumentTitle], out type)) {
                            return;
                    }
                    var linkPath = parameters[CreateLinkPathArgumentTitle];

                    if(Directory.Exists(linkPath))
                        Directory.Delete(linkPath, true);
                    else if(File.Exists(linkPath))
                        File.Delete(linkPath);

                    var success = CreateSymbolicLink(parameters[CreateLinkPathArgumentTitle], parameters[CreateDestinationArgumentTitle], type);
                    if (!success) {
                        var error = Marshal.GetLastWin32Error();
                        #if DEBUG
                            Debugger.Break();
                        #endif
                    }
                    break;
                }
            }
        }