【问题标题】:Is possible to define custom URI scheme used system-wide?是否可以定义系统范围内使用的自定义 URI 方案?
【发布时间】:2016-08-24 22:00:57
【问题描述】:

很长一段时间以来,我一直在想是否可以创建一个库来充当 Windows 中的文件提供程序。

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

Asynchronous Pluggable Protocol 可以解决吗?我无法找到有关如何使其工作的工作代码示例。

要理解:我需要它在系统范围内工作。现在已连接到 Internet 浏览器。

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

【问题讨论】:

标签: c# c++ windows com uri-scheme


【解决方案1】:

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 对象,该对象处理执行该本地复制的逻辑。

如果您只是希望通过转到像 steam 那样使用 steam://rungameid/382110 的 URL 来打开您的应用程序,您只需要按照 this MSDN page 中的说明进行操作即可。

如果您希望您的文件可以通过http:ftp: 之类的外壳“打开”打开文件对话框,您将需要编写一个充当“源过滤器”的 COM 对象,我不知道任何地方可以找到这方面的文档。

更新:阅读更多内容,它看起来确实像Asynchronous Pluggable Protocol,就像您提到的那样,您是如何制作这些源过滤器的。我从来没有尝试过做一个,所以我无法帮助你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多