可以定义在系统范围内使用的自定义URI方案

本文关键字:自定义 URI 方案 范围内 定义 系统 | 更新日期: 2023-09-27 18:03:47

我一直在想是否有可能在Windows中创建一个库作为文件提供程序。

例如,如果您尝试使用标准的打开文件对话框打开ftp://example.com/touch.txt文件,它(以某种方式神奇地)工作。是否有一种方法可以为我自己的URI方案实现我自己的提供者?

Asynchronous Pluggable Protocol可能是一个解决方案吗?我无法找到一个关于如何使其工作的工作代码示例。

要理解:我需要这个在系统范围内工作。这是没有连接到互联网浏览器。

如果我需要这个File.Open("my://test.txt")工作呢?

可以定义在系统范围内使用的自定义URI方案

File.ReadAllBytes("ftp://example.com/touch.txt");不工作,如果你尝试它,你会得到一个异常,如

System.NotSupportedException was unhandled
  Message=The given path's format is not supported.
  Source=mscorlib
  StackTrace:
       at System.Security.Util.StringExpressionSet.CanonicalizePath(String path, Boolean needFullPath)
       at System.Security.Util.StringExpressionSet.CreateListFromExpressions(String[] str, Boolean needFullPath)
       at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
       at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, AccessControlActions control, String[] pathList, Boolean checkForDuplicates, Boolean needFullPath)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
       at System.IO.File.ReadAllBytes(String path)
       at WindowsFormsApplication1.Form1.button1_Click(Object sender, EventArgs e) in D:'Code'WindowsFormsApplication1'WindowsFormsApplication1'Form1.cs:line 25
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at WindowsFormsApplication1.Program.Main() in D:'Code'WindowsFormsApplication1'WindowsFormsApplication1'Program.cs:line 17
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

它工作的原因,当你做一个OpenFileDialog是windows欺骗,并将下载文件到本地临时文件夹首先。它只对ftp:http:这样做,因为OpenFileDialog是由windows shell处理的,shell是使用URI方案的。

我相信HKCR'ftp下的Source Filter键指向一个注册的COM对象,该对象处理进行本地复制的逻辑。

如果你只是想让你的应用程序通过一个URL打开,就像steam对它的steam://rungameid/382110一样,你只需要遵循这个MSDN页面中的说明。

如果你想要你的文件是"可打开的"通过shell像http:ftp:与打开文件对话框,你需要写一个COM对象作为"源过滤器",我不知道有任何地方可以找到文档。

Update:阅读更多,它看起来像异步可插拔协议,就像你提到的是你如何制作那些源过滤器。我从来没有尝试过制作一个,所以我不能帮助你超越。