【问题标题】:Image to the created file图像到创建的文件
【发布时间】:2017-02-07 09:32:10
【问题描述】:

我们可以将图像或位图设置为已创建或现有的文本、xml 或 word 文件吗?

如果是这样,请告诉我如何为文件设置位图或图像。

我想更改文件的默认位图。

问候, 阿马尔

【问题讨论】:

标签: c# image winforms file


【解决方案1】:

要更改特定文件扩展名的图标,请使用以下代码在注册表中输入所有需要的信息:

using Microsoft.Win32;

// Associate file extension with progID, description, icon and application
public static void Associate(string extension, string progID, string description, string icon, string application)
{
   Registry.ClassesRoot.CreateSubKey(extension).SetValue("", progID);
   if (progID!=null && progID.Length>0)
      using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(progID))
      {
         if (description!=null)
            key.SetValue("", description);
         if (icon!=null)
            key.CreateSubKey("DefaultIcon").SetValue("", ToShortPathName(icon));
         if (application!=null)
            key.CreateSubKey(@"Shell\Open\Command").SetValue("", ToShortPathName(application) + " \"%1\"");
      }
}

// Return true if extension already associated in registry
public static bool IsAssociated(string extension)
{
   return (Registry.ClassesRoot.OpenSubKey(extension, false)!=null);
}

[DllImport("Kernel32.dll")]
private static extern uint GetShortPathName(string lpszLongPath, [Out] StringBuilder lpszShortPath, uint cchBuffer);

// Return short path format of a file name
private static string ToShortPathName(string longName)
{
   StringBuilder s = new StringBuilder(1000);
   uint iSize = (uint) s.Capacity;
   uint iRet = GetShortPathName(longName, s, iSize);
   return s.ToString();
}

这样称呼它:

if (!IsAssociated(".ext"))
   Associate(".ext", "ClassID.ProgID", "ext File", "YourIcon.ico", "YourApplication.exe");

(来自https://www.codeproject.com/Articles/621/Associate-File-Extension-with-Shell-OPEN-command-a?msg=1589623#xx1589623xx

此外,如果您还可以更改任何给定文件夹的图标。这不是你要求的,但如果你想了解更多,我建议你看看这篇文章:https://www.codeproject.com/Articles/9331/Create-Icons-for-Folders-in-Windows-Explorer-Using

【讨论】:

  • 我不知道为 Associate() 传递参数。
  • 什么意思?字符串扩展名 => 要更改 ie 图标的文件类型的文件扩展名。 '.txt' 或 '.doc',字符串 progID => 将其保留为“ClassId.ProgId”,字符串描述 => 任何你想要的 - 它是文件类型的描述,字符串图标 => 图标的路径,字符串应用程序 => 应用程序的路径
  • 您提到了扩展名作为文件类型。但是注册表是使用 Associate() 方法中提供的扩展打开的,这是无法完成的
  • 无法打开注册表,因为扩展中没有注册表
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-07
  • 1970-01-01
  • 1970-01-01
  • 2014-04-26
  • 2018-05-22
  • 2010-11-12
  • 2012-09-28
相关资源
最近更新 更多