using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class IconHelper
{
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
private static extern int SHGetFileInfo(string pszPath, int dwFileAttributes, out SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
private const int FILE_ATTRIBUTE_DIRECTORY = 0x10;
private const int SHGFI_ICON = 0x100;
private const int SHGFI_SMALLICON = 0x1;
private const int SHGFI_LARGEICON = 0x0;
[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
}
public static void SetFolderIcon(string folderPath, string iconPath)
{
if (!Directory.Exists(folderPath))
{
throw new DirectoryNotFoundException();
}
if (!File.Exists(iconPath))
{
throw new FileNotFoundException();
}
SHFILEINFO shfi = new SHFILEINFO();
SHGetFileInfo(iconPath, 0, out shfi, (uint)Marshal.SizeOf(shfi), SHGFI_ICON);
using (Icon icon = Icon.FromHandle(shfi.hIcon))
{
using (FileStream stream = new FileStream(Path.Combine(folderPath, "desktop.ini"), FileMode.Create))
{
using (StreamWriter writer = new StreamWriter(stream))
{
writer.WriteLine("[.ShellClassInfo]");
writer.WriteLine("IconResource=" + iconPath.Replace("\\", "\\\\") + ",0");
writer.Flush();
}
}
}
File.SetAttributes(Path.Combine(folderPath, "desktop.ini"), FileAttributes.ReadOnly | FileAttributes.Hidden);
File.SetAttributes(folderPath, File.GetAttributes(folderPath) | FileAttributes.System);
}
}
public static class Program
{
public static void Main()
{
string folderPath = "مسار المجلد";
string iconPath = "مسار الايكونة";
try
{
IconHelper.SetFolderIcon(folderPath, iconPath);
MessageBox.Show("تم تعيين الأيقونة بنجاح");
}
catch (DirectoryNotFoundException)
{
MessageBox.Show("المجلد غير موجود");
}
catch (FileNotFoundException)
{
MessageBox.Show("الأيقونة غير موجودة");
}
catch (Exception ex)
{
MessageBox.Show("حدث خطأ غير متوقع: " + ex.Message);
}
}
}
قم بتعديل المتغيرين folderPath و iconPath في دالة Main واستبدل القيم التجريبية بالمسارات الفعلية للمجلد والأيقونة التي ترغب في تعيينهما. يجب استدعاء دالة SetFolderIcon لتعيين الأيقونة للمجلد.
بالطبع! يمكنك تنسيق النص ليكون أكثر وضوحًا عن طريق استخدام تنسيق الشفرة. فيما يلي الكود المنسق بشكل صحيح:
csharp
using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class IconHelper
{
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
private static extern int SHGetFileInfo(string pszPath, int dwFileAttributes, out SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
private const int FILE_ATTRIBUTE_DIRECTORY = 0x10;
private const int SHGFI_ICON = 0x100;
private const int SHGFI_SMALLICON = 0x1;
private const int SHGFI_LARGEICON = 0x0;
[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
}
public static void SetFolderIcon(string folderPath, string iconPath)
{
if (!Directory.Exists(folderPath))
{
throw new DirectoryNotFoundException();
}
if (!File.Exists(iconPath))
{
throw new FileNotFoundException();
}
SHFILEINFO shfi = new SHFILEINFO();
SHGetFileInfo(iconPath, 0, out shfi, (uint)Marshal.SizeOf(shfi), SHGFI_ICON);
using (Icon icon = Icon.FromHandle(shfi.hIcon))
{
using (FileStream stream = new FileStream(Path.Combine(folderPath, "desktop.ini"), FileMode.Create))
{
using (StreamWriter writer = new StreamWriter(stream))
{
writer.WriteLine("[.ShellClassInfo]");
writer.WriteLine("IconResource=" + iconPath.Replace("\\", "\\\\") + ",0");
writer.Flush();
}
}
}
File.SetAttributes(Path.Combine(folderPath, "desktop.ini"), FileAttributes.ReadOnly | FileAttributes.Hidden);
File.SetAttributes(folderPath, File.GetAttributes(folderPath) | FileAttributes.System);
}
}
public static class Program
{
public static void Main()
{
string folderPath = "مسار المجلد";
string iconPath = "مسار الايكونة";
try
{
IconHelper.SetFolderIcon(folderPath, iconPath);
MessageBox.Show("تم تعيين الأيقونة بنجاح");
}
catch (DirectoryNotFoundException)
{
MessageBox.Show("المجلد غير موجود");
}
catch (FileNotFoundException)
{
MessageBox.Show("الأيقونة غير موجودة");
}
catch (Exception ex)
{
MessageBox.Show("حدث خطأ غير متوقع: " + ex.Message);
}
}
}
لاحظ أنه تم تنسيق الشفرة بشكل صحيح لزيادة قابلية القراءة.
أعتذر عن الخطأ السابق. ها هو الكود المنسق بشكل صحيح:
csharp
using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class IconHelper
{
[DllImport("shell32.dll", CharSet = CharSet.Unicode)]
private static extern int SHGetFileInfo(string pszPath, int dwFileAttributes, out SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
private const int FILE_ATTRIBUTE_DIRECTORY = 0x10;
private const int SHGFI_ICON = 0x100;
private const int SHGFI_SMALLICON = 0x1;
private const int SHGFI_LARGEICON = 0x0;
[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
}
public static void SetFolderIcon(string folderPath, string iconPath)
{
if (!Directory.Exists(folderPath))
{
throw new DirectoryNotFoundException();
}
if (!File.Exists(iconPath))
{
throw new FileNotFoundException();
}
SHFILEINFO shfi = new SHFILEINFO();
SHGetFileInfo(iconPath, 0, out shfi, (uint)Marshal.SizeOf(shfi), SHGFI_ICON);
using (Icon icon = Icon.FromHandle(shfi.hIcon))
{
using (FileStream stream = new FileStream(Path.Combine(folderPath, "desktop.ini"), FileMode.Create))
{
using (StreamWriter writer = new StreamWriter(stream))
{
writer.WriteLine("[.ShellClassInfo]");
writer.WriteLine("IconResource=" + iconPath.Replace("\\", "\\\\") + ",0");
writer.Flush();
}
}
}
File.SetAttributes(Path.Combine(folderPath, "desktop.ini"), FileAttributes.ReadOnly | FileAttributes.Hidden);
File.SetAttributes(folderPath, File.GetAttributes(folderPath) | FileAttributes.System);
}
}
public static class Program
{
public static void Main()
{
string folderPath = "مسار المجلد";
string iconPath = "مسار الايقونة";
try
{
IconHelper.SetFolderIcon(folderPath, iconPath);
MessageBox.Show("تم تعيين الأيقونة بنجاح");
}
catch (DirectoryNotFoundException)
{
MessageBox.Show("المجلد غير موجود");
}
catch (FileNotFoundException)
{
MessageBox.Show("الأيقونة غير موجودة");
}
catch (Exception ex)
{
MessageBox.Show("حدث خطأ غير متوقع: " + ex.Message);
}
}
}