Exemple #1
0
        public bool Attach(FileCall file, string alias = null)
        {
            if (!IsAttachable) //check attachablility
            {
                throw new InvalidOperationException("Unable to attach using this method. avoid calling before checking.");
            }

            var alias_resolve = (string.IsNullOrEmpty(alias)
                    ? file.GetFileNameWithoutExtension()
                    : alias);

            if (Attached.Any(attfile => attfile.FullName.Equals(file.FullName))) //check if already set to startup.
            {
                throw new InvalidOperationException("This file is already attached.");
            }
            using (RegistryKey add = Registry.LocalMachine.OpenSubKey(RegistryKeyPath, true)) {
                if (add == null)
                {
                    throw new Exception("Invalid registery path/not found.");
                }

                try {
                    add.SetValue(alias_resolve, "\"" + file.FullName + "\"" + (string.IsNullOrEmpty(DefaultArguments) ? "" : (" " + DefaultArguments)));
                } catch {
                    return(false);
                }
                return(true);
            }
        }
        public bool Attach(FileCall file, string alias = null)
        {
            if (!IsAttachable)
            {
                throw new InvalidOperationException("Unable to attach using this method. avoid calling before checking.");
            }
            if (Attached.Any(attfile => attfile.FullName.Equals(file.FullName))) //check if already set to startup.
            {
                throw new InvalidOperationException("This file is already attached.");
            }
            var link = (IShellLink) new ShellLink();

            link.SetDescription("My Description");
            link.SetPath(file.FullName);

            string targetPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup),
                                             ((string.IsNullOrEmpty(alias)
                                                    ? file.GetFileNameWithoutExtension()
                                                    : alias) + ".lnk"));

            if (File.Exists(targetPath)) //validate file doesnt exist before saving.
            {
                throw new InvalidOperationException("The name of the supposed shortcut is already taken for another file. change the alias name");
            }

            ((IPersistFile)link).Save(targetPath, false);

            return(File.Exists(targetPath));
        }