NEWS

[C#] Thay đổi Icon mặc định cho Folder trong lập trình Winform

[C#] Thay đổi Icon mặc định cho Folder trong lập trình Winform
Đăng bởi: Thảo Meo - Lượt xem: 7320 12:43:07, 06/07/2020PHẦN MỀM

Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn cách thay đổi hình ảnh Icon cho Folder của Window Explorer trong lập trình C# winform.

[C#] Change Icon Default folder Windows

Để thay đổi icon của một folder trong Windows, chúng ta sẽ sử dụng 1 file Desktop.ini, để thay đổi icon.

Cấu trúc file của Desktop.ini thay đổi Icon như hình bên dưới:

[.ShellClassInfo]
ConfirmFileOp=True
NoSharing=True
IconFile=C:\Users\nguyenthao\Downloads\Hopstarter-Soft-Scraps-Email-Download.ico
IconIndex=0

IconFile là đường dẫn chỉ tới icon để thay đổi.

Giao diện demo ứng dụng thay đổi Icon Folder C#:

icon_folder

Full source code C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SetIconFolder
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_selectFolder_Click(object sender, EventArgs e)
        {
            using (var fbd = new FolderBrowserDialog())
            {
                DialogResult result = fbd.ShowDialog();

                if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
                {
                    txtFolderInput.Text = fbd.SelectedPath;
                }
            }
        }

        private void btn_SelectIcon_Click(object sender, EventArgs e)
        {
            using (var dlg = new OpenFileDialog())
            {
                DialogResult result = dlg.ShowDialog();
                if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(dlg.FileName))
                {
                    txtIconInput.Text = dlg.FileName;
                    pic_icon.LoadAsync(dlg.FileName);
                }
            }
        }

        private void btn_create_Click(object sender, EventArgs e)
        {
            var success = CreateIconedFolder(txtFolderInput.Text, txtIconInput.Text, 0, "", true, true);
            if (success)
            {
                MessageBox.Show("Create Icon Successful!");
            }
            
        }

        private bool CreateIconedFolder(string folderName, string iconFile, int iconIndex, string toolTip,
            bool preventSharing, bool confirmDelete)
        {
          
            DirectoryInfo folder;
            string fileName = "desktop.ini";

            if (Directory.Exists(folderName) == false)
            {
                return false;
            }

            try
            {
                folder = new DirectoryInfo(folderName);
                fileName = Path.Combine(folderName, fileName);

                // Create the file
                using (StreamWriter sw = new StreamWriter(fileName))
                {
                    sw.WriteLine("[.ShellClassInfo]");
                    sw.WriteLine("ConfirmFileOp={0}", confirmDelete);
                    sw.WriteLine("NoSharing={0}", preventSharing);
                    sw.WriteLine("IconFile={0}", iconFile);
                    sw.WriteLine("IconIndex={0}", iconIndex);
                    sw.WriteLine("InfoTip={0}", toolTip);
                    sw.Close();
                }
             
                folder.Attributes = folder.Attributes | FileAttributes.System;              
                File.SetAttributes(fileName, File.GetAttributes(fileName) | FileAttributes.Hidden);
            }
            catch
            {
                return false;
            }

            return true;
        }

        private bool UndoIconedFolder(string folderName)
        {          
            DirectoryInfo folder;         
            if (Directory.Exists(folderName) == false)
            {
                return false;
            }         

            try
            {
                folder = new DirectoryInfo(folderName);              
                FileInfo file = new FileInfo(Path.Combine(folderName, "desktop.ini"));
                if (file.Exists)
                {
                    file.Delete();
                }
                folder.Attributes = (folder.Attributes | FileAttributes.System);
            }
            catch
            {
                return false;
            }

            return true;
        }

        private void btn_delete_Click(object sender, EventArgs e)
        {
            bool result = UndoIconedFolder(txtFolderInput.Text);
        }
    }
}

Sau khi chọn hình ảnh và thay đổi xong, các bạn nhé Restart lại Windows Explorer để xóa cache để hiển thị Icon cho folder.

Thanks for watching!

DOWNLOAD SOURCE

Tags: change icon folder c#icon folder c#

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Thay đổi Icon mặc định cho Folder trong lập trình Winform
Đăng bởi: Thảo Meo - Lượt xem: 7320 12:43:07, 06/07/2020PHẦN MỀM