深入了解createassociation

一、创建桌面快捷方式

createassociation函数是一个用于在Windows操作系统中创建关联的函数。在Windows操作系统中,当我们双击某个文件时,系统会根据不同文件类型的扩展名来调用不同的应用程序。如果我们想要把某个应用程序的文件关联为某个扩展名的文件,则需要使用到createassociation函数。

此处通过创建桌面快捷方式的实例来说明createassociation函数的使用:

    Shell objShell = new Shell();
    string filePath = @"C:\Windows\System32\notepad.exe"; //需要创建快捷方式的程序路径
    string shortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Notepad.lnk"; //快捷方式保存路径
    IWshShortcut shortcut = (IWshShortcut)objShell.CreateShortcut(shortcutPath); //创建快捷方式对象
    shortcut.TargetPath = filePath; //设置目标路径
    shortcut.WorkingDirectory = Path.GetDirectoryName(filePath); //设置工作目录
    shortcut.WindowStyle = 1; //设置窗口样式
    shortcut.Description = "Notepad Shortcut"; //设置描述信息
    shortcut.Save(); //保存快捷方式

二、文件类型关联

除了创建桌面快捷方式外,createassociation函数还可以用于文件类型关联。例如你想让系统把.mp3文件关联为某个音乐播放器程序,则可以使用createassociation函数来实现。下面是一个简单的文件类型关联的示例:

    [DllImport("shell32.dll")]
    static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
    [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
    static extern uint AssocCreateForClasses([In] ref Guid clsid, [In] ref Guid riid, [Out, MarshalAs(UnmanagedType.Interface)] out object ppv);
    [DllImport("shlwapi.dll", CharSet = CharSet.Unicode)]
    static extern int AssocSetPerceivedType([In] ref Guid clsid, [In] uint type);
    static void Associate(string ext, string appName)
    {
        Guid clsid = new Guid("YOUR_APPLICATION_CLSID");
        Guid riid = new Guid("YOUR_IASSOCWSTR_CLSID");
        object obj;
        AssocCreateForClasses(ref clsid, ref riid, out obj);
        IAssocWSTR assocStr = (IAssocWSTR)obj;
        uint key = 0;
        assocStr.GetKeyValue(new IntPtr(0), key, "PerceivedType", out key);
        AssocSetPerceivedType(ref clsid, key);
        SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
    }
    Associate(".mp3", "Music Player");

三、设置默认打开方式

使用createassociation函数可以轻松地将一个程序设置为某个文件类型的默认打开方式,例如把Windows Media Player设置为MP4文件的默认打开方式。

    string extension = ".mp4";
    string progId = "WMPlayer";
    string friendlyName = "Windows Media Player";
    try
    {
        Registry.ClassesRoot.CreateSubKey(extension).SetValue(null, progId);
        Registry.ClassesRoot.CreateSubKey(progId).SetValue(null, friendlyName);
        Registry.ClassesRoot.CreateSubKey(progId + @"\DefaultIcon").SetValue(null, @"%ProgramFiles%\Windows Media Player\wmplayer.exe,0");
        Registry.ClassesRoot.CreateSubKey(progId + @"\shell\open\command").SetValue(null, @"C:\Program Files\Windows Media Player\wmplayer.exe /open ""%1""");
    }
    catch
    {
        Console.WriteLine("An error occurred while attempting to set file associations.");
    }

四、修改打开方式

除了设置默认打开方式外,createassociation函数还可以用于修改某个文件类型的打开方式。例如你想把某个视频文件的默认播放方式从Windows Media Player改为VLC Media Player,可以使用createassociation函数来实现:

    string fileExtension = ".mp4";
    string progId = "VLC.mp4";
    try 
    {
        Registry.ClassesRoot.CreateSubKey(fileExtension).SetValue(null, progId);
        Registry.ClassesRoot.CreateSubKey(progId).CreateSubKey("shell").CreateSubKey("open").CreateSubKey("command").SetValue("", "\"" + vlcPath + "\" \"%1\"");
    }
    catch 
    {
        Console.WriteLine("An error occurred while attempting to set file associations.");
    }

五、总结

createassociation是Windows操作系统中一个常用的函数,用于创建文件关联、设置默认打开方式、修改打开方式等操作。以上通过不同实例来说明createassociation的使用方法,希望能够帮助大家掌握此函数的使用。

原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/298078.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝的头像小蓝
上一篇 2024-12-28 12:17
下一篇 2024-12-28 12:17

相关推荐

  • 深入解析Vue3 defineExpose

    Vue 3在开发过程中引入了新的API `defineExpose`。在以前的版本中,我们经常使用 `$attrs` 和` $listeners` 实现父组件与子组件之间的通信,但…

    编程 2025-04-25
  • 深入理解byte转int

    一、字节与比特 在讨论byte转int之前,我们需要了解字节和比特的概念。字节是计算机存储单位的一种,通常表示8个比特(bit),即1字节=8比特。比特是计算机中最小的数据单位,是…

    编程 2025-04-25
  • 深入理解Flutter StreamBuilder

    一、什么是Flutter StreamBuilder? Flutter StreamBuilder是Flutter框架中的一个内置小部件,它可以监测数据流(Stream)中数据的变…

    编程 2025-04-25
  • 深入探讨OpenCV版本

    OpenCV是一个用于计算机视觉应用程序的开源库。它是由英特尔公司创建的,现已由Willow Garage管理。OpenCV旨在提供一个易于使用的计算机视觉和机器学习基础架构,以实…

    编程 2025-04-25
  • 深入了解scala-maven-plugin

    一、简介 Scala-maven-plugin 是一个创造和管理 Scala 项目的maven插件,它可以自动生成基本项目结构、依赖配置、Scala文件等。使用它可以使我们专注于代…

    编程 2025-04-25
  • 深入了解LaTeX的脚注(latexfootnote)

    一、基本介绍 LaTeX作为一种排版软件,具有各种各样的功能,其中脚注(footnote)是一个十分重要的功能之一。在LaTeX中,脚注是用命令latexfootnote来实现的。…

    编程 2025-04-25
  • 深入探讨冯诺依曼原理

    一、原理概述 冯诺依曼原理,又称“存储程序控制原理”,是指计算机的程序和数据都存储在同一个存储器中,并且通过一个统一的总线来传输数据。这个原理的提出,是计算机科学发展中的重大进展,…

    编程 2025-04-25
  • 深入了解Python包

    一、包的概念 Python中一个程序就是一个模块,而一个模块可以引入另一个模块,这样就形成了包。包就是有多个模块组成的一个大模块,也可以看做是一个文件夹。包可以有效地组织代码和数据…

    编程 2025-04-25
  • 深入剖析MapStruct未生成实现类问题

    一、MapStruct简介 MapStruct是一个Java bean映射器,它通过注解和代码生成来在Java bean之间转换成本类代码,实现类型安全,简单而不失灵活。 作为一个…

    编程 2025-04-25
  • 深入理解Python字符串r

    一、r字符串的基本概念 r字符串(raw字符串)是指在Python中,以字母r为前缀的字符串。r字符串中的反斜杠(\)不会被转义,而是被当作普通字符处理,这使得r字符串可以非常方便…

    编程 2025-04-25

发表回复

登录后才能评论