是否可以定义全系统使用的自定义URI方案?

问题描述:

我一直想知道是否有可能创建一个在Windows中充当文件提供程序的库。是否可以定义全系统使用的自定义URI方案?

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

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

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

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

+0

完全可能,你在正确的轨道上。看看https://msdn.microsoft.com/en-us/library/aa767914%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396 – itsme86

+0

样本可在这里https:// blogs。 msdn.microsoft.com/noahc/2006/10/19/register-a-custom-url-protocol-handler/ –

+0

好的。这使我可以在调用URI时打开我的应用程序。但是如果我想通过这个URI传递一个对象呢?例如File.Read( “我://test.txt”); –

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: 

当你做一个打开文件对话框的窗口骗子与和将文件下载到本地临时工作原理的原因异常首先文件夹。它只对ftp:http:执行此操作,因为OpenFileDialog由Windows Shell处理,而Shell是使用URI方案的。

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

如果您只想通过访问网址来打开您的应用程序,就好像蒸汽一样,它就是steam://rungameid/382110您只需按照this MSDN page中的说明操作即可。

如果你希望你的文件是“打开”通过像http:ftp:一个外壳,打开文件对话框没有你需要写充当“源过滤器”的COM对象,我不知道任何找到相关文档。

更新:阅读更多它看起来像Asynchronous Pluggable Protocol像你提到的是你如何使这些源过滤器。我从来没有尝试过,所以我无法帮助你。