You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows平台Electron实现文件管理器覆盖图标与右键菜单求助

Electron云同步应用:覆盖图标与右键菜单实现问题

需求概述

  • 基于Electron JS开发云同步应用,将本地数据同步至云端
  • 需实现两个核心功能:
    • 为文件/文件夹添加类似Google Drive、OneDrive的状态覆盖图标(对勾/叉号)
    • 在文件管理器中为目录添加右键菜单,支持添加/移除同步资产

已尝试的解决方案及问题

1. npm包尝试

测试过多个npm包实现覆盖图标和右键菜单功能,均未达到预期效果。

2. winreg修改注册表

使用winreg@1.2.4包修改注册表来实现覆盖图标,未成功。

3. 创建desktop.ini文件

通过代码生成desktop.ini文件,无效,代码如下:

fs.writeFileSync(desktopIconPath,
                `[ViewState]
                Mode=
                FolderType=Pictures
                Vid=
                
                [.ShellClassInfo]
                IconResource=${desktopicon}
                IconIndex=103
                  `);

4. C# DLL + edge-js调用(System.Runtime加载失败)

创建C# DLL后,尝试用edge-js@19.3.0在Node.js中调用,出现无法加载System.Runtime的错误:

{ Error: Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. at Error (native) at Object.exports.func (D:\Kunal Someskar\Test Practice Projects\testelectronApp\node_modules\edge-js\lib\edge.js:178:17)   at App.app.on.error (D:\Kunal Someskar\Test Practice Projects\testelectronApp\src\main.js:71:39) at emitTwo (events.js:111:20) at App.emit (events.js:191:7) Message: 'Could not load file or assembly \'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\' or one of its dependencies. The system cannot find the file specified.', FileName: 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a', FusionLog: 'WRN: Assembly binding logging is turned OFF.\r\nTo enable assembly bind failure logging, set the registry value [HKLM\\Software\\Microsoft\\Fusion!EnableLog] (DWORD) to 1.\r\nNote: There is some performance penalty associated with assembly bind failure logging.\r\nTo turn this feature off, remove the registry value [HKLM\\Software\\Microsoft\\Fusion!EnableLog].\r\n', Data: {}, InnerException: null, TargetSite: {}, StackTrace: '   at System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type)\r\n   at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)\r\n   at ClrFuncReflectionWrap.Create(Assembly assembly, String typeName, String methodName) in D:\\Kunal Someskar\\Test Practice Projects\\testelectronApp\\node_modules\\edge-js\\src\\dotnet\\clrfuncreflectionwrap.cpp:line 11\r\n   at ClrFunc.Initialize(FunctionCallbackInfo<v8::Value>* info) in D:\\Kunal Someskar\\Test Practice Projects\\testelectronApp\\node_modules\\edge-js\\src\\dotnet\\clrfunc.cpp:line 101', HelpLink: null, Source: 'mscorlib', HResult: -2147024894, message: 'Could not load file or assembly \'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\' or one of its dependencies. The system cannot find the file specified.', name: 'System.IO.FileNotFoundException' }

DLL代码

using SharpShell.Interop;
using SharpShell.SharpIconOverlayHandler;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Runtime;

namespace ClassLibrary1
{
    /// <summary>
    /// The ReadOnlyFileIconOverlayHandler is an IconOverlayHandler that shows
    /// a padlock icon over files that are read only.
    /// </summary>
    [ComVisible(true)]
    public class ReadOnlyFileIconOverlayHandler : SharpIconOverlayHandler
    {
        /// <summary>
        /// Called by the system to get the priority, which is used to determine
        /// which icon overlay to use if there are multiple handlers. The priority
        /// must be between 0 and 100, where 0 is the highest priority.
        /// </summary>
        /// <returns>
        /// A value between 0 and 100, where 0 is the highest priority.
        /// </returns>
        protected override int GetPriority()
        {
            //  The read only icon overlay is very low priority
            return 90;
        }

        /// <summary>
        /// Determines whether an overlay should be shown for the shell item 
        /// with the path 'path' and
        /// the shell attributes 'attributes'
        /// </summary>
        ///// <param name="path">The path for the shell item. This is not necessarily the path
        /// to a physical file or folder.</param>
        /// <param name="attributes">The attributes of the shell item.</param>
        /// <returns>
        ///   <c>true</c> if this an overlay should be shown for the specified item; 
        ///  otherwise, <c>false</c>.
        /// </returns>
        protected override bool CanShowOverlay(string path, FILE_ATTRIBUTE attributes)
        {
            try
            {
                //  Get the file attributes
                var fileAttributes = new FileInfo(path);

                //  Return true if the file is read only, meaning we'll show the overlay
                return fileAttributes.IsReadOnly;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Called to get the icon to show as the overlay icon
        /// </summary>
        /// <returns>
        /// The overlay icon
        /// </returns>
        protected override System.Drawing.Icon GetOverlayIcon()
        {
            //  Return the read only icon
            Icon icon = new Icon("Resources/correct.ico");
            return icon;
        }

    }

    public class test
    {
        public static int Add(int a, int b)
        {
            return a + b;
        }
    }
}

edge-js调用代码

try {
    var helloDll = require('edge-js').func({
      assemblyFile: "src/ClassLibrary1.dll",
      typeName: "ClassLibrary1.test",
      methodName: "Add"
    });
    console.log(helloDll);
  } catch (error) {
    console.log(error)
  }

内容的提问来源于stack exchange,提问作者Kunal Someskar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 19:02:52