- [C#] Bắt sự kiện bàn phím chuột bên ngoài ứng dụng winform sử dụng thư viện MouseKeyHook
- [DEVEXPRESS] Đặt mật khẩu và bỏ mật khẩu tập tin file PDF
- [C#] Thêm ứng dụng vào Taskbar sử dụng SharpShell DeskBand
- [C#] Hướng dẫn thêm text vào hình ảnh icon winform
- [C#] Chia sẽ tổng hợp source code đồ án về Csharp
- [C#] Hướng dẫn viết ứng dụng quay màn hình video winform, Screen Recorder
- [C#] Hướng dẫn sử dụng thư viện Input Simulator để làm việc với Keyboard, Mouse Virtual
- [DEVEXPRESS] Hướng dẫn tích Filter Contain khi click chuột phải vào cell selection trên Gridview
- [C#] Tra cứu mã số thuế cá nhân bằng CMND hoặc CCCD
- [C#] Convert hình ảnh image thành Blurhash sử dụng trong loading image winform
- [POWERSHELL] Script sao lưu backup và nén database sqlserver
- [C#] Giới thiệu thư viện Autofac Dependency Injection
- [C#] Hướng dẫn tạo Windows Services đơn giản Winform
- [C#] Một click chuột điều khiển máy tính từ xa sử dụng Ultraviewer
- Hướng dẫn đóng gói phần mềm sử dụng Powershell biên dịch script thành file exe
- [C#] Hướng dẫn sử dụng Task Dialog trên NET 5
- [C#] Hướng dẫn xem lịch sử các trang web đã truy cập trên Chrome Browser
- [C#] Hướng dẫn lấy thông tin Your ID và Password của Ultraviewer Winform
- [C#] Hướng dẫn lấy thông tin Your ID và Password của Teamviewer Winform
- [C#] Hướng dẫn build code động xuất file exe trên Winform
[C#] Thay đổi Icon mặc định cho Folder trong lập trình Winform
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#:
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!