/// <summary> /// Processes the button click. /// </summary> /// <param name="sender">Sender.</param> /// <param name="e">E.</param> private void OnProcessButtonClick(object sender, EventArgs e) { // Declare processed count int processedCount = 0; // Iterate nodes foreach (TreeNode node in this.iconTreeView.Nodes) { // Set settings LPSHFOLDERCUSTOMSETTINGS FolderCustomSettings = new LPSHFOLDERCUSTOMSETTINGS { dwMask = 0x10, pszIconFile = node.ImageKey, iIconIndex = 0 }; // Set new icon UInt32 HRESULT = SHGetSetFolderCustomSettings(ref FolderCustomSettings, node.Name, FCS_FORCEWRITE); // Raise processed count processedCount++; } // Set processed count property this.ProcessedCount = processedCount; // Close form this.Close(); }
/// <summary> /// Handles the restore folder icons button click event. /// </summary> /// <param name="sender">Sender object.</param> /// <param name="e">Event arguments.</param> private void OnRestoreFolderIconsButtonClick(object sender, EventArgs e) { // Show folder browser dialog if (this.folderBrowserDialog.ShowDialog() == DialogResult.OK && this.folderBrowserDialog.SelectedPath.Length > 0) { // Declare processed count int processedCount = 0; // Set sbudirectories list var subdirectoryList = Directory.GetDirectories(this.folderBrowserDialog.SelectedPath, "*", SearchOption.TopDirectoryOnly); // Iterate subdirectories foreach (var subdirectory in subdirectoryList) { // Get .exe file(s) List <string> exeFileList = Directory.GetFiles(subdirectory, "*.exe", SearchOption.TopDirectoryOnly).ToList(); // Check for no exe file if (exeFileList.Count == 0) { // Halt flow continue; } // Error handling & logging try { // Set settings LPSHFOLDERCUSTOMSETTINGS FolderCustomSettings = new LPSHFOLDERCUSTOMSETTINGS { dwMask = FCSM_ICONFILE, pszIconFile = null, cchIconFile = 0, iIconIndex = 0 }; // Set default icon UInt32 HRESULT = SHGetSetFolderCustomSettings(ref FolderCustomSettings, subdirectory, FCS_FORCEWRITE); // Raise count processedCount++; } catch (Exception ex) { // Log error event this.logEvent.WriteEvent("Folder icon restore failed", $"Directory: {subdirectory}{Environment.NewLine}Message: {ex.Message}"); } } // Update processed count this.countToolStripStatusLabel.Text = processedCount.ToString(); // Advise user MessageBox.Show("Folder icons restored to default.", $"Restored {processedCount} icons", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
private void RefreshSystemFile() { LPSHFOLDERCUSTOMSETTINGS FolderSettings = new LPSHFOLDERCUSTOMSETTINGS(); FolderSettings.dwMask = 0x40; uint FCS_FORCEWRITE = 0x00000002; var pszPath = Path.GetDirectoryName(filePath); _ = SHGetSetFolderCustomSettings(ref FolderSettings, pszPath, FCS_FORCEWRITE); }
static public void SetIcoToFolder(string Folder, string IconFile) { LPSHFOLDERCUSTOMSETTINGS FolderSettings = new LPSHFOLDERCUSTOMSETTINGS(); FolderSettings.dwMask = 0x10; FolderSettings.pszIconFile = IconFile; FolderSettings.iIconIndex = 1; UInt32 FCS_READ = 0x00000001; UInt32 FCS_FORCEWRITE = 0x00000002; UInt32 FCS_WRITE = FCS_READ | FCS_FORCEWRITE; string pszPath = Folder; UInt32 HRESULT = SHGetSetFolderCustomSettings(ref FolderSettings, pszPath, FCS_WRITE); }
public static uint RemoveFolderIcon(string targetFolderPath) { LPSHFOLDERCUSTOMSETTINGS FolderSettings = new LPSHFOLDERCUSTOMSETTINGS(); FolderSettings.dwMask = FCSM_ICONFILE; FolderSettings.pszIconFile = null; FolderSettings.iIconIndex = 0; string pszPath = targetFolderPath; uint HRESULT = SHGetSetFolderCustomSettings(ref FolderSettings, pszPath, FCS_WRITE); return(HRESULT); }
private void changeIcon2(string path, int index) { try { if (File.Exists(path + @"\desktop.ini")) { File.Delete(path + @"\desktop.ini"); } } catch { } if (index == 7) { LPSHFOLDERCUSTOMSETTINGS FolderSettings = new LPSHFOLDERCUSTOMSETTINGS(); FolderSettings.dwMask = 0x10; FolderSettings.pszIconFile = @"C:\colorIcons.icl"; FolderSettings.iIconIndex = 0; UInt32 FCS_READ = 0x00000001; UInt32 FCS_FORCEWRITE = 0x00000002; UInt32 FCS_WRITE = FCS_READ | FCS_FORCEWRITE; string pszPath = path; UInt32 HRESULT = SHGetSetFolderCustomSettings(ref FolderSettings, pszPath, FCS_WRITE); try { if (File.Exists(path + @"\desktop.ini")) { File.Delete(path + @"\desktop.ini"); } } catch { } } else { LPSHFOLDERCUSTOMSETTINGS FolderSettings = new LPSHFOLDERCUSTOMSETTINGS(); FolderSettings.dwMask = 0x10; FolderSettings.pszIconFile = @"C:\colorIcons.icl"; FolderSettings.iIconIndex = index; UInt32 FCS_READ = 0x00000001; UInt32 FCS_FORCEWRITE = 0x00000002; UInt32 FCS_WRITE = FCS_READ | FCS_FORCEWRITE; string pszPath = path; UInt32 HRESULT = SHGetSetFolderCustomSettings(ref FolderSettings, pszPath, FCS_WRITE); //Console.WriteLine(HRESULT.ToString("x")); //Console.ReadLine(); } }
static public void ChangeIcon(string folder, string icon) { LPSHFOLDERCUSTOMSETTINGS FolderSettings = new LPSHFOLDERCUSTOMSETTINGS(); FolderSettings.dwMask = 0x10; FolderSettings.pszIconFile = @icon; FolderSettings.iIconIndex = 0; UInt32 FCS_READ = 0x00000001; UInt32 FCS_FORCEWRITE = 0x00000002; UInt32 FCS_WRITE = FCS_READ | FCS_FORCEWRITE; string pszPath = @folder; UInt32 HRESULT = SHGetSetFolderCustomSettings(ref FolderSettings, pszPath, FCS_WRITE); }
public void SetIcon(string folder, string iconPath) { Console.WriteLine($"Writing icon {iconPath} to folder {folder}"); LPSHFOLDERCUSTOMSETTINGS FolderSettings = new LPSHFOLDERCUSTOMSETTINGS(); FolderSettings.dwMask = 0x10; FolderSettings.pszIconFile = iconPath; FolderSettings.iIconIndex = 0; UInt32 FCS_READ = 0x00000001; UInt32 FCS_FORCEWRITE = 0x00000002; UInt32 FCS_WRITE = FCS_READ | FCS_FORCEWRITE; UInt32 HRESULT = SHGetSetFolderCustomSettings(ref FolderSettings, folder, FCS_WRITE); }
//Sets the icon for a specific folder using desktop.ini // Special Thanks to Mike Zhang[MSFT] static void SetFolderIcon(string path) { Console.WriteLine("Setting Folder Icon"); //folder path LPSHFOLDERCUSTOMSETTINGS FolderSettings = new LPSHFOLDERCUSTOMSETTINGS(); FolderSettings.dwMask = 0x10; FolderSettings.pszIconFile = @path + "\\icon.ico"; FolderSettings.iIconIndex = 0; UInt32 FCS_READ = 0x00000001; UInt32 FCS_FORCEWRITE = 0x00000002; UInt32 FCS_WRITE = FCS_READ | FCS_FORCEWRITE; string pszPath = path; UInt32 HRESULT = SHGetSetFolderCustomSettings(ref FolderSettings, pszPath, FCS_WRITE); //Console.WriteLine(HRESULT.ToString("x")); //Console.ReadLine(); }
private static void SetFolderIcon(string FolderPath) { try { Logger.Out("Setting Icon To Folder: {0}", FolderPath); LPSHFOLDERCUSTOMSETTINGS FolderSettings = new LPSHFOLDERCUSTOMSETTINGS(); FolderSettings.dwMask = 0x10; FolderSettings.pszIconFile = "folder.ico"; UInt32 FCS_READ = 0x00000001; UInt32 FCS_FORCEWRITE = 0x00000002; UInt32 FCS_WRITE = FCS_READ | FCS_FORCEWRITE; string pszPath = FolderPath;//@"G:\FreeZone\New folder\New folder"; UInt32 HRESULT = SHGetSetFolderCustomSettings(ref FolderSettings, pszPath, FCS_FORCEWRITE); } catch (Exception ex) { // log exception } }
public static extern UInt32 SHGetSetFolderCustomSettings(ref LPSHFOLDERCUSTOMSETTINGS pfcs, string pszPath, UInt32 dwReadWrite);
static extern UInt32 SHGetSetFolderCustomSettings(ref LPSHFOLDERCUSTOMSETTINGS pfcs, string pszPath, UInt32 dwReadWrite);
private static extern uint SHGetSetFolderCustomSettings(ref LPSHFOLDERCUSTOMSETTINGS pfcs, string pszPath, uint dwReadWrite);