在 Windows 上使用 Electron Js 在文件管理器中实现覆盖图标或上下文菜单选项

问题描述 投票:0回答:1

我们正在 Electron JS 中创建一个同步应用程序,它将用户计算机数据与云同步。我们正在尝试实现两个功能,其中之一是在文件或目录(打勾或交叉)上添加覆盖图标,就像 Google Drive 或 One Drive 那样。另一个功能是我们想在文件管理器中为一个目录添加一个上下文菜单选项,这将允许用户从文件管理器本身内部添加或删除同步资产。

我们尝试了各种 npm 包来尝试在用户文件或目录上实现覆盖图标或添加上下文菜单选项。

我们还尝试显式添加一些注册表值,以便使用 npm winreg(^1.2.4) 包实现覆盖图标,但这对我们不起作用。

我们甚至尝试从我们的代码本身创建一个 desktop.ini 文件,但仍然没有用。附代码。

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

最后我们尝试创建一个 C# DLL 并尝试使用 npm edge-js(^19.3.0) 通过节点 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)
  }
c# node.js dll electron edgejs
1个回答
0
投票

错误是System.Runtime.dll 无法加载,但我不知道EdgeJs。您可以将 System.Runtime.dll 作为依赖项放置吗?

根据github自述文件,你应该添加

#r "System.Runtime.dll"
还有
SharpShell.dll
我相信......

© www.soinside.com 2019 - 2024. All rights reserved.