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);
        }
        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.CurrentUser.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;
            }
        }
Beispiel #3
0
        /// <summary>
        ///     Attaches the file to the best prioritized startup method.
        /// </summary>
        /// <param name="filename">The file that will start on startup</param>
        /// <param name="alias">The name inwhich the startup will be registered under, used to avoid collisions and can be null.</param>
        public static StartupAttachResult AttachBestNative(FileCall filename, string alias=null) {
            alias = alias ?? filename.Alias;
            var methods = GetWorkingNativeStartupMethods();
            if (methods.Any(method=>method.IsAttached(filename))) return StartupAttachResult.AlreadyAttached;
            StartupAttachResult @result = StartupAttachResult.None;
            if (methods.Count==0) return StartupAttachResult.NoMethodsAvailable;
            foreach (var method in methods) {
                try {
                    method.Attach(filename, alias);
#if DEBUG
                    Console.WriteLine("Attached "+alias+":"+filename.FullName+" to method "+method);
#endif
                    return StartupAttachResult.Successful;
                } catch (InvalidOperationException) {
                    @result = StartupAttachResult.FailedWhileAttaching;
                } catch (Exception e) {
                    throw e; //throw unhandled.
                }
            }
            return @result;
        }