C# 如何以编程方式更改有效的windows图标?

C# 如何以编程方式更改有效的windows图标?,c#,powershell,binary,icons,C#,Powershell,Binary,Icons,我需要创建10万个不同的测试图标,可以在C#或powershell中。 实际上,我有10k个同名的图标文件,我想我可以很容易地读入二进制图标,转换成字节,注入一些随机数,然后写回一个文件,但它不像我看到的那样工作 $fi = @(Get-ChildItem D:\icons -rec | ForEach-Object -Process {$_.FullName}) # | select -first $amount) $no = 0 foreach($i in $fi) { $

我需要创建10万个不同的测试图标,可以在C#或powershell中。 实际上,我有10k个同名的图标文件,我想我可以很容易地读入二进制图标,转换成字节,注入一些随机数,然后写回一个文件,但它不像我看到的那样工作

$fi = @(Get-ChildItem  D:\icons -rec  | ForEach-Object -Process {$_.FullName})  # | select -first $amount) 
$no = 0

foreach($i in $fi)
{
    $array =  Read-FileByte $i;
    $array = $array + [System.Text.Encoding]::UTF8.GetBytes($no) 
    [System.IO.File]::WriteAllBytes($i, $array) 
    $no++
}
在此代码运行之后,windows仍然认为图标相同

另一种方法是通过编程方式创建有效的10k图标, 有办法做到这一点吗?
谢谢

您可以使用此代码生成任意数量的随机图标:

using System;
using System.Linq;
using System.Drawing;
using System.IO;
static class Program
{
    [STAThread]
    static void Main()
    {
        var gen = new RandomIconGenerator(32);
        var dir = new DirectoryInfo(@"C:\RandIcons\");
        if (!dir.Exists) dir.Create();
        for (int it = 0; it < 1000; it++)
            using (var s = new FileStream(@"C:\RandIcons\" + "icon-" + it + ".ico", FileMode.Create))
                gen.MakeRandomIcon().Save(s);
    }
}
/// <summary>
/// Generates random icons using various colored shapes and lines, using available brushes and pens.
/// </summary>
public class RandomIconGenerator
{
    Random r = new Random();
    Pen[] pens = typeof(Pens).GetProperties().Select(p => (Pen)p.GetValue(null, null)).ToArray();
    Brush[] brushes = typeof(Brushes).GetProperties().Select(p => (Brush)p.GetValue(null, null)).ToArray();
    int size;
    public RandomIconGenerator(int size) { this.size = size; }
    public Icon MakeRandomIcon()
    {
        using (Bitmap bmp = new Bitmap(size, size))
        using (Graphics g = Graphics.FromImage(bmp))
        {
            for (int it = 0; it < 20; it++) this.GetRandomPainter()(g);
            g.Flush();
            return Icon.FromHandle(bmp.GetHicon());
        }
    }
    private Pen GetRandomPen() { return this.pens[this.r.Next(this.pens.Length)]; }
    private Brush GetRandomBrush() { return this.brushes[this.r.Next(this.brushes.Length)]; }
    private Action<Graphics> GetRandomPainter()
    {
        switch (r.Next(5))
        {
            case 0: return g => g.DrawLine(this.GetRandomPen(), this.GetRandomPoint(), this.GetRandomPoint());
            case 1: return g => g.DrawRectangle(this.GetRandomPen(), this.GetRandomRect());
            case 2: return g => g.DrawEllipse(this.GetRandomPen(), this.GetRandomRect());
            case 3: return g => g.FillRectangle(this.GetRandomBrush(), this.GetRandomRect());
            case 4: return g => g.FillEllipse(this.GetRandomBrush(), this.GetRandomRect());
            default: throw new Exception();
        }
    }
    private Rectangle GetRandomRect()
    {
        var p0 = this.GetRandomPoint();
        return new Rectangle(p0, new Size(this.GetRandomPoint()) - new Size(p0));
    }
    private int GetRandomPos() { return this.r.Next(this.size); }
    private Point GetRandomPoint() { return new Point(this.GetRandomPos(), this.GetRandomPos()); }
}
使用系统;
使用System.Linq;
使用系统图;
使用System.IO;
静态类程序
{
[状态线程]
静态void Main()
{
var gen=新的随机发电机(32);
var dir=new DirectoryInfo(@“C:\RandIcons\”);
如果(!dir.Exists)dir.Create();
for(int it=0;it<1000;it++)
使用(var s=newfilestream(@“C:\RandIcons\“+”icon-“+it+”.ico”,FileMode.Create))
gen.MakeRandomIcon().Save;
}
}
/// 
///使用各种颜色的形状和线条,使用可用的画笔和钢笔生成随机图标。
/// 
公共类随机生成器
{
随机r=新随机();
Pen[]pens=typeof(pens).GetProperties().Select(p=>(Pen)p.GetValue(null,null)).ToArray();
笔刷[]笔刷=类型(笔刷).GetProperties()。选择(p=>(笔刷)p.GetValue(null,null)).ToArray();
整数大小;
公共随机数生成器(int size){this.size=size;}
公共图标MakeRandomIcon()
{
使用(位图bmp=新位图(大小、大小))
使用(Graphics g=Graphics.FromImage(bmp))
{
对于(int it=0;it<20;it++)this.GetRandomPainter()(g);
g、 冲洗();
返回Icon.FromHandle(bmp.GetHicon());
}
}
private Pen GetRandomPen(){返回this.pens[this.r.Next(this.pens.Length)];}
private Brush GetRandomBrush(){返回this.Brush[this.r.Next(this.Brush.Length)];}
私人行动
{
开关(右下(5))
{
案例0:返回g=>g.DrawLine(this.GetRandomPen(),this.GetRandomPoint(),this.GetRandomPoint());
案例1:返回g=>g.DrawRectangle(this.GetRandomPen(),this.GetRandomRect());
案例2:返回g=>g.Dropellipse(this.GetRandomPen(),this.GetRandomRect());
案例3:返回g=>g.FillRectangle(this.GetRandomBrush(),this.GetRandomRect());
案例4:返回g=>g.FillEllipse(this.GetRandomBrush(),this.GetRandomRect());
默认值:抛出新异常();
}
}
私有矩形GetRandomRect()
{
var p0=this.GetRandomPoint();
返回新矩形(p0,新大小(this.GetRandomPoint())-新大小(p0));
}
private int GetRandomPos(){返回this.r.Next(this.size);}
私有点GetRandomPoint(){返回新点(this.GetRandomPos(),this.GetRandomPos());}
}

您可以使用此代码生成任意数量的随机图标:

using System;
using System.Linq;
using System.Drawing;
using System.IO;
static class Program
{
    [STAThread]
    static void Main()
    {
        var gen = new RandomIconGenerator(32);
        var dir = new DirectoryInfo(@"C:\RandIcons\");
        if (!dir.Exists) dir.Create();
        for (int it = 0; it < 1000; it++)
            using (var s = new FileStream(@"C:\RandIcons\" + "icon-" + it + ".ico", FileMode.Create))
                gen.MakeRandomIcon().Save(s);
    }
}
/// <summary>
/// Generates random icons using various colored shapes and lines, using available brushes and pens.
/// </summary>
public class RandomIconGenerator
{
    Random r = new Random();
    Pen[] pens = typeof(Pens).GetProperties().Select(p => (Pen)p.GetValue(null, null)).ToArray();
    Brush[] brushes = typeof(Brushes).GetProperties().Select(p => (Brush)p.GetValue(null, null)).ToArray();
    int size;
    public RandomIconGenerator(int size) { this.size = size; }
    public Icon MakeRandomIcon()
    {
        using (Bitmap bmp = new Bitmap(size, size))
        using (Graphics g = Graphics.FromImage(bmp))
        {
            for (int it = 0; it < 20; it++) this.GetRandomPainter()(g);
            g.Flush();
            return Icon.FromHandle(bmp.GetHicon());
        }
    }
    private Pen GetRandomPen() { return this.pens[this.r.Next(this.pens.Length)]; }
    private Brush GetRandomBrush() { return this.brushes[this.r.Next(this.brushes.Length)]; }
    private Action<Graphics> GetRandomPainter()
    {
        switch (r.Next(5))
        {
            case 0: return g => g.DrawLine(this.GetRandomPen(), this.GetRandomPoint(), this.GetRandomPoint());
            case 1: return g => g.DrawRectangle(this.GetRandomPen(), this.GetRandomRect());
            case 2: return g => g.DrawEllipse(this.GetRandomPen(), this.GetRandomRect());
            case 3: return g => g.FillRectangle(this.GetRandomBrush(), this.GetRandomRect());
            case 4: return g => g.FillEllipse(this.GetRandomBrush(), this.GetRandomRect());
            default: throw new Exception();
        }
    }
    private Rectangle GetRandomRect()
    {
        var p0 = this.GetRandomPoint();
        return new Rectangle(p0, new Size(this.GetRandomPoint()) - new Size(p0));
    }
    private int GetRandomPos() { return this.r.Next(this.size); }
    private Point GetRandomPoint() { return new Point(this.GetRandomPos(), this.GetRandomPos()); }
}
使用系统;
使用System.Linq;
使用系统图;
使用System.IO;
静态类程序
{
[状态线程]
静态void Main()
{
var gen=新的随机发电机(32);
var dir=new DirectoryInfo(@“C:\RandIcons\”);
如果(!dir.Exists)dir.Create();
for(int it=0;it<1000;it++)
使用(var s=newfilestream(@“C:\RandIcons\“+”icon-“+it+”.ico”,FileMode.Create))
gen.MakeRandomIcon().Save;
}
}
/// 
///使用各种颜色的形状和线条,使用可用的画笔和钢笔生成随机图标。
/// 
公共类随机生成器
{
随机r=新随机();
Pen[]pens=typeof(pens).GetProperties().Select(p=>(Pen)p.GetValue(null,null)).ToArray();
笔刷[]笔刷=类型(笔刷).GetProperties()。选择(p=>(笔刷)p.GetValue(null,null)).ToArray();
整数大小;
公共随机数生成器(int size){this.size=size;}
公共图标MakeRandomIcon()
{
使用(位图bmp=新位图(大小、大小))
使用(Graphics g=Graphics.FromImage(bmp))
{
对于(int it=0;it<20;it++)this.GetRandomPainter()(g);
g、 冲洗();
返回Icon.FromHandle(bmp.GetHicon());
}
}
private Pen GetRandomPen(){返回this.pens[this.r.Next(this.pens.Length)];}
private Brush GetRandomBrush(){返回this.Brush[this.r.Next(this.Brush.Length)];}
私人行动
{
开关(右下(5))
{
案例0:返回g=>g.DrawLine(this.GetRandomPen(),this.GetRandomPoint(),this.GetRandomPoint());
案例1:返回g=>g.DrawRectangle(this.GetRandomPen(),this.GetRandomRect());
案例2:返回g=>g.Dropellipse(this.GetRandomPen(),this.GetRandomRect());
案例3:返回g=>g.FillRectangle(this.GetRandomBrush(),this.GetRandomRect());
案例4:返回g=>g.FillEllipse(this.GetRandomBrush(),this.GetRandomRect());
默认值:抛出新异常();
}
}
私有矩形GetRandomRect()
{
var p0=this.GetRandomPoint();
返回新矩形(p0,新大小(this.GetRandomPoint())-新大小(p0));
}
private int GetRandomPos(){返回this.r.Next(this.size);}
私有点GetRandomPoint(){返回新点(this.GetRandomPos(),this.GetRandomPos());}
}
//已修复
[DllImport(“user32.dll”,SetLastError=true)]静态外部布尔销毁图标(IntPtr hIcon);
公共图标MakeRandomIcon()
{
使用(位图bmp=新位图(大小、大小))
使用(Graphics g=Graphics.FromImage(bmp))
{
对于(int it=0;it<20;it++)this.GetRandomPainter()(g);
g、 处置();
IntPtr hIcon=bmp.GetHicon();
Icon temp=图标FromHandle(hIcon);
图标图标=