图像到创建的文件

时间:2017-02-07 09:32:11

标签: c# image winforms file

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

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

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

enter image description here

此致 Amal

1 个答案:

答案 0 :(得分:0)

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

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