/// <summary> /// Fills a list with shortcuts items /// </summary> /// <param name="type"></param> void DisplayStartupShortcuts(string type) { bool disabled; string command; string filePath; string folder; if (type == rm.GetString("StartupCurrentUser")) { // Current users startup folder. folder = Environment.GetFolderPath(Environment.SpecialFolder.Startup); } else { // All users startup folder. folder = Environment.ExpandEnvironmentVariables("%AllUsersProfile%") + @"\Start Menu\Programs\Startup"; } try { foreach (string shortcut in Directory.GetFiles(folder, "*.*", SearchOption.AllDirectories)) { // Only process shortcuts, executibles, or disabled shortcuts. if (Path.GetExtension(shortcut) == ".lnk" || Path.GetExtension(shortcut) == ".exe") { // Set diabled flag. if (Path.GetDirectoryName(shortcut).EndsWith("~Disabled")) { disabled = true; } else { disabled = false; } // Save complete command. command = shortcut; // Save file path. filePath = ReturnFilePath(command); // Resolve the shortcut. var sc = new ShortcutClass(filePath); // Attempt to get application image (icon). if (sc.Icon != null) { // First try getting icon from shortcut. imageListStartupManager.Images.Add(sc.Icon); } else if (native.GetIcon(sc.Path) != null) { // Then try getting icon from the resolved path. imageListStartupManager.Images.Add(native.GetIcon(sc.Path)); } else { // If both methods fail, display a blank icon. imageListStartupManager.Images.Add((Image)resourceManager.GetObject("Blank")); } // Add entry description to listview. ListViewItem lvi = new ListViewItem(Path.GetFileNameWithoutExtension(shortcut)); lvi.ImageIndex = i; // Add file name (without path) to listview. lvi.SubItems.Add(Path.GetFileName(filePath)); // Add type information to listview. lvi.SubItems.Add(type == rm.GetString("StartupCurrentUser") ? rm.GetString("StartupCurrentUser") : rm.GetString("StartupAllUsers")); // Add status information. lvi.SubItems.Add(disabled ? rm.GetString("disabled") : rm.GetString("enabled")); // Add command. lvi.SubItems.Add(command); // Add file path. lvi.SubItems.Add(filePath); listviewStartup.Items.Add(lvi); i++; } } } catch (Exception) { //Directory may not exist. } }
/// <summary> /// Runs current item /// </summary> /// <param name="index">item index in list</param> void ExecuteCommand(int index) { try { string fileName; string arguments; // Get the file name with path. fileName = listviewStartup.Items[index].SubItems[(int)ListCol.Path].Text; // If the file is a shortcut, resolve it; otherwise use filename. if (Path.GetExtension(fileName) == ".lnk") { var sc = new ShortcutClass(fileName); fileName = sc.Path; arguments = sc.Arguments; sc.Dispose(); } else { // Get the complete command. string command = listviewStartup.Items[index].SubItems[(int)ListCol.Command].Text; // If the command contains a complete path and filename, remove to get the arguments. if (command.Contains(@"\:")) { arguments = command.Replace(fileName, string.Empty); } // Check if filename in "command" does not contain an extension. // Just remove the filename to get the arguments. else if (!command.Substring(0, command.IndexOf(" ") - 1).Contains(".")) { arguments = command.Replace(Path.GetFileNameWithoutExtension(fileName), string.Empty); } else { arguments = command.Replace(Path.GetFileName(fileName), string.Empty); } // Remove all quotes. arguments = arguments.Replace("\"", string.Empty); } if (File.Exists(fileName)) { // Start program. var startInfo = new ProcessStartInfo(fileName); startInfo.Arguments = arguments; startInfo.WindowStyle = ProcessWindowStyle.Normal; Process.Start(startInfo); } else { // Cannot find file. MessageBox.Show(rm.GetString("file_folder_not_found"), rm.GetString("startup_manager"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } catch (Exception ex) { if (ex is Win32Exception) { MessageBox.Show(rm.GetString("file_folder_not_found"), rm.GetString("startup_manager"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } else { MessageBox.Show(string.Format("{0}{1}{2}{3}{4}", rm.GetString("unable_to_execute"), Environment.NewLine, rm.GetString("system_returned"), Environment.NewLine, ex.Message), rm.GetString("startup_manager"), MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
/// <summary> /// Handles listViewStartup ItemSelectionChanged event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void listviewStartup_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) { //Show Details Section firstCover.Visible = false; string command = String.Empty; string filePath = String.Empty; try { if (e.IsSelected) { // Make the selected index available. currentIndex = e.ItemIndex; // Get command and file path stored in listview and make available panel wide. command = listviewStartup.Items[e.ItemIndex].SubItems[(int)ListCol.Command].Text; // Trim command if it is too long if (command.Length > 128) { string[] splittedCommand = command.Split('\\'); if (splittedCommand.Length > 3) { command = String.Format(@"{0}\{1}\...\{2}\{3}", splittedCommand[0], splittedCommand[1], splittedCommand[splittedCommand.Length - 2], splittedCommand[splittedCommand.Length - 1]); } } filePath = listviewStartup.Items[e.ItemIndex].SubItems[(int)ListCol.Path].Text; // Make sure the filePath exists. if (File.Exists(filePath)) { // Determine if file has hidden or system attribute. if (((File.GetAttributes(filePath) & FileAttributes.Hidden) == FileAttributes.Hidden) || ((File.GetAttributes(filePath) & FileAttributes.System) == FileAttributes.System)) { // Change to normal. File.SetAttributes(filePath, FileAttributes.Normal); } // Display the file information. if (filePath.Contains("cmd.exe")) { // Since this is a command window, we will not be able to resolve any properties. labelCompany.Text = string.Empty; labelProductName.Text = string.Empty; labelDescription.Text = string.Empty; labelFileVersion.Text = string.Empty; labelCommand.Text = command; // Only display arguments for shortcuts. labelArguments.Visible = false; labelArguments.Text = string.Empty; } else if (Path.GetExtension(filePath) == ".lnk") { // Resolve the shortcut. ShortcutClass sc = new ShortcutClass(filePath); // Get the file version information. FileVersionInfo selectedFileVersionInfo = FileVersionInfo.GetVersionInfo(sc.Path); // Display the resolved shortcut properties. labelCompany.Text = selectedFileVersionInfo.CompanyName; labelProductName.Text = selectedFileVersionInfo.ProductName; labelDescription.Text = selectedFileVersionInfo.FileDescription; labelFileVersion.Text = selectedFileVersionInfo.FileVersion; labelCommand.Text = command; // Display arguments for shortcuts, but only if present. if (string.IsNullOrEmpty(sc.Arguments)) { labelArguments.Visible = false; } else { labelArguments.Visible = true; labelArguments.Text = sc.Arguments; } //Takal // Attempt to get application image (icon). if (sc.Icon != null) { // First try getting icon from shortcut. pictureBoxPanel.Image = GetBitmap(sc.Icon); } else if (native.GetIcon(sc.Path) != null) { // Then try getting icon from the resolved path. pictureBoxPanel.Image = GetBitmap(native.GetIcon(sc.Path)); } // Dispose of the class instance. sc.Dispose(); } else { // Get the file version information. FileVersionInfo selectedFileVersionInfo = FileVersionInfo.GetVersionInfo(filePath); // Display the file properties. labelCompany.Text = selectedFileVersionInfo.CompanyName; labelProductName.Text = selectedFileVersionInfo.ProductName; labelDescription.Text = selectedFileVersionInfo.FileDescription; labelFileVersion.Text = selectedFileVersionInfo.FileVersion; labelCommand.Text = command; //Takal if (native.GetIcon(filePath) != null) { pictureBoxPanel.Image = GetBitmap(native.GetIcon(filePath)); } else { pictureBoxPanel.Image = null; } // Only display arguments for shortcuts. labelArguments.Visible = false; labelArguments.Text = string.Empty; } // Set state of context menu and tool strip buttons depending upon location and user rights. if ((listviewStartup.Items[e.ItemIndex].SubItems[(int)ListCol.Type].Text.Contains("Current User")) || (listviewStartup.Items[e.ItemIndex].SubItems[(int)ListCol.Type].Text.Contains("Aktueller Benutzer"))) { SetItems(e.ItemIndex, "Current User"); } else if ((listviewStartup.Items[e.ItemIndex].SubItems[(int)ListCol.Type].Text.Contains("All Users")) || (listviewStartup.Items[e.ItemIndex].SubItems[(int)ListCol.Type].Text.Contains("Alle Benutzer"))) { // Do not allow non admins to do anything in All Users. if (admin == false) { toolStripMenuItemEnable.Enabled = false; toolStripButtonEnable.Enabled = false; toolStripMenuItemDisable.Enabled = false; toolStripButtonDisable.Enabled = false; toolStripMenuItemDelete.Enabled = false; toolStripButtonDelete.Enabled = false; toolStripMenuItemExecute.Enabled = false; toolStripButtonExecute.Enabled = false; toolStripMenuItemOpen.Enabled = false; toolStripButtonOpen.Enabled = false; toolStripMenuItemMoveToAllUsers.Enabled = false; toolStripButtonMoveToAllUsers.Enabled = false; toolStripMenuItemMoveToCurrentUser.Enabled = false; toolStripButtonMoveToCurrentUser.Enabled = false; } else { SetItems(e.ItemIndex, "All Users"); } } } else { // Disable all context menu items and buttons except delete. toolStripMenuItemEnable.Enabled = false; toolStripButtonEnable.Enabled = false; toolStripMenuItemDisable.Enabled = false; toolStripButtonDisable.Enabled = false; toolStripMenuItemDelete.Enabled = true; toolStripButtonDelete.Enabled = true; toolStripMenuItemExecute.Enabled = false; toolStripButtonExecute.Enabled = false; toolStripMenuItemOpen.Enabled = false; toolStripButtonOpen.Enabled = false; toolStripMenuItemMoveToAllUsers.Enabled = false; toolStripButtonMoveToAllUsers.Enabled = false; toolStripMenuItemMoveToCurrentUser.Enabled = false; toolStripButtonMoveToCurrentUser.Enabled = false; // Clear all information, since the file is missing or invalid. labelCompany.Text = String.Empty; labelProductName.Text = String.Empty; labelDescription.Text = String.Empty; labelFileVersion.Text = String.Empty; labelCommand.Text = String.Empty; // Only display arguments for shortcuts. labelArguments.Visible = false; labelArguments.Text = String.Empty; // Display a message to the user indicating that the file does not exist. MessageBox.Show(string.Format("{0}{1}{2}", rm.GetString("file_invalid"), Environment.NewLine, rm.GetString("commands_disabled")), rm.GetString("startup_manager"), MessageBoxButtons.OK, MessageBoxIcon.Error); } } } catch (SecurityException ex) { MessageBox.Show(string.Format("{0}{1}{2}{3}{4} {5}", rm.GetString("no_permission"), Environment.NewLine, rm.GetString("system_returned"), Environment.NewLine, rm.GetString("description"), ex.Message), rm.GetString("startup_manager"), MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (FileNotFoundException) { MessageBox.Show(string.Format("{0}{1}{2} {3}{4}{5}: {6}", rm.GetString("file_not_found"), Environment.NewLine, rm.GetString("file_path"), filePath, Environment.NewLine, rm.GetString("command"), command), rm.GetString("startup_manager"), MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } catch (IOException exc) { MessageBox.Show(exc.Message, rm.GetString("startup_manager"), MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (ArgumentException excp) { MessageBox.Show(string.Format("{0}{1}{2} {3}{4}{5}: {6}", excp.Message, Environment.NewLine, rm.GetString("file_path"), filePath, Environment.NewLine, rm.GetString("command"), command), rm.GetString("startup_manager"), MessageBoxButtons.OK, MessageBoxIcon.Error); } }