Register a Custom URL Protocol Handler[转]
2009-11-28 21:38
621 查看
Register a Custom URL Protocol Handler
Perhaps you're familiar with such URL protocol handlers such as http://microsoft.com
, ftp://user:pass@server
outlook:Contacts
, file://http://www.coad.net/blog/images/Greenstone.bmp
, ms-help://...
Want to make your own client-side URL handler? Maybe to pull data
from a source and display it, share content between applications,
launch apps from web links, etc?
Say you want to be able to link from your intranet to special
resources you know are on your internal network, or render data between
applications via a central server across platforms and technologies.
This is one way to do some of that…
Sample Application
Here is a sample app that is
designed to run registered applications on the local machine using a
keyword. After installing the example RunAppUrlProtocol
app, you can put links on web pages like runapp://calc
, runapp://excel
, or runapp://docs
that
will trigger this app, look up the keyword, and execute the app. You
can install the app and then click the links to try it.
Registering the URL Protocol Handler
The key part here is the string value “URL Protocol” (with a blank value) which tells Windows to register this as a handler.
[HKEY_CLASSES_ROOT]
[runapp]
(Default) = "URL:RunApp Protocol Handler"
URL Protocol = ""
[DefaultIcon]
(Default) = "c:/whatever/RunAppUrlProtocol.exe”
[shell]
[open]
[command]
(Default) = "c:/whatever/RunAppUrlProtocol.exe "%1""
The Code
Here is a sample app that demonstrates writing your own handler, RunAppUrlProtocol.zip
. The key elements here are:
This started as a Console Application that I changed the project
properties to a Windows Application so that a console window isn’t
opened up. This allows the app to run “hidden”.
Grabbing the protocol from the command line args, so “runapp://excel” would be passed as a command argument.
The use of Regular Expressions here is trivial, but you can imagine
it would be a lot more helpful when the URL is more complex, like the
FTP URL with many optional components. ftp://[<username>][:<password>]@<server>[:<port>][/<resource>]
This example has a minimal amount of user input checking, any
decent app would have a lot more (for example, checking to make sure
the target exists before trying to run it)
A good way to extend this example would be to add support for
command line arguments in the URL, like
runapp://<appkey>/<args>
#region
Namespace Inclusions
using
System
;
using
System
.IO
;
using
System
.Xml
;
using
System
.Reflection
;
using
System
.Diagnostics
;
using
System
.Windows
.Forms
;
using
System
.Text
.RegularExpressions
;
#endregion
namespace
RunAppUrlProtocol
{
class
Program
{
static
void
Main
(string
[] args
)
{
// The URL handler for this app
string
prefix
= "runapp://"
;
// The name of this app for user messages
string
title
= "RunApp URL Protocol Handler"
;
// Verify the command line arguments
if
(args
.Length
== 0 || !args
[0].StartsWith
(prefix
))
{ MessageBox
.Show
("Syntax:/nrunapp://<key>"
, title
); return
; }
// Obtain the part of the protocol we're interested in
string
key
= Regex
.Match
(args
[0], @"(?<=://).+?(?=:|/|/Z)"
).Value
;
// Path to the configuration file
string
file
= Path
.Combine
(Path
.GetDirectoryName
(
Assembly
.GetExecutingAssembly
().Location
), "RegisteredApps.xml"
);
// Load the config file
XmlDocument
xml
= new
XmlDocument
();
xml
.Load
(file
);
// Locate the app to run
XmlNode
node
= xml
.SelectSingleNode
(
String
.Format
("/RunApp/App[@key='{0}']"
, key
));
// If the app is not found, let the user know
if
(node
== null
)
{ MessageBox
.Show
("Key not found: "
+ key
, title
); return
; }
// Resolve the target app name
string
target
= Environment
.ExpandEnvironmentVariables
(
node
.SelectSingleNode
("@target"
).Value
);
// Pull the command line args for the target app if they exist
string
procargs
= node
.SelectSingleNode
("@args"
) != null
?
node
.SelectSingleNode
("@args"
).Value
: ""
;
// Start the application
Process
.Start
(target
, procargs
);
}
}
}
References
VSS: protocol handler for Visual SourceSafe
MSDN: Registering an Application to a URL Protocol
C# Source Code: RunAppUrlProtocol.zip
, ftp://user:pass@server
outlook:Contacts
, file://http://www.coad.net/blog/images/Greenstone.bmp
,
Perhaps you're familiar with such URL protocol handlers such as http://microsoft.com
, ftp://user:pass@server
outlook:Contacts
, file://http://www.coad.net/blog/images/Greenstone.bmp
, ms-help://...
Want to make your own client-side URL handler? Maybe to pull data
from a source and display it, share content between applications,
launch apps from web links, etc?
Say you want to be able to link from your intranet to special
resources you know are on your internal network, or render data between
applications via a central server across platforms and technologies.
This is one way to do some of that…
Sample Application
Here is a sample app that is
designed to run registered applications on the local machine using a
keyword. After installing the example RunAppUrlProtocol
app, you can put links on web pages like runapp://calc
, runapp://excel
, or runapp://docs
that
will trigger this app, look up the keyword, and execute the app. You
can install the app and then click the links to try it.
Registering the URL Protocol Handler
The key part here is the string value “URL Protocol” (with a blank value) which tells Windows to register this as a handler.
[HKEY_CLASSES_ROOT]
[runapp]
(Default) = "URL:RunApp Protocol Handler"
URL Protocol = ""
[DefaultIcon]
(Default) = "c:/whatever/RunAppUrlProtocol.exe”
[shell]
[open]
[command]
(Default) = "c:/whatever/RunAppUrlProtocol.exe "%1""
The Code
Here is a sample app that demonstrates writing your own handler, RunAppUrlProtocol.zip
. The key elements here are:
This started as a Console Application that I changed the project
properties to a Windows Application so that a console window isn’t
opened up. This allows the app to run “hidden”.
Grabbing the protocol from the command line args, so “runapp://excel” would be passed as a command argument.
The use of Regular Expressions here is trivial, but you can imagine
it would be a lot more helpful when the URL is more complex, like the
FTP URL with many optional components. ftp://[<username>][:<password>]@<server>[:<port>][/<resource>]
This example has a minimal amount of user input checking, any
decent app would have a lot more (for example, checking to make sure
the target exists before trying to run it)
A good way to extend this example would be to add support for
command line arguments in the URL, like
runapp://<appkey>/<args>
#region
Namespace Inclusions
using
System
;
using
System
.IO
;
using
System
.Xml
;
using
System
.Reflection
;
using
System
.Diagnostics
;
using
System
.Windows
.Forms
;
using
System
.Text
.RegularExpressions
;
#endregion
namespace
RunAppUrlProtocol
{
class
Program
{
static
void
Main
(string
[] args
)
{
// The URL handler for this app
string
prefix
= "runapp://"
;
// The name of this app for user messages
string
title
= "RunApp URL Protocol Handler"
;
// Verify the command line arguments
if
(args
.Length
== 0 || !args
[0].StartsWith
(prefix
))
{ MessageBox
.Show
("Syntax:/nrunapp://<key>"
, title
); return
; }
// Obtain the part of the protocol we're interested in
string
key
= Regex
.Match
(args
[0], @"(?<=://).+?(?=:|/|/Z)"
).Value
;
// Path to the configuration file
string
file
= Path
.Combine
(Path
.GetDirectoryName
(
Assembly
.GetExecutingAssembly
().Location
), "RegisteredApps.xml"
);
// Load the config file
XmlDocument
xml
= new
XmlDocument
();
xml
.Load
(file
);
// Locate the app to run
XmlNode
node
= xml
.SelectSingleNode
(
String
.Format
("/RunApp/App[@key='{0}']"
, key
));
// If the app is not found, let the user know
if
(node
== null
)
{ MessageBox
.Show
("Key not found: "
+ key
, title
); return
; }
// Resolve the target app name
string
target
= Environment
.ExpandEnvironmentVariables
(
node
.SelectSingleNode
("@target"
).Value
);
// Pull the command line args for the target app if they exist
string
procargs
= node
.SelectSingleNode
("@args"
) != null
?
node
.SelectSingleNode
("@args"
).Value
: ""
;
// Start the application
Process
.Start
(target
, procargs
);
}
}
}
References
VSS: protocol handler for Visual SourceSafe
MSDN: Registering an Application to a URL Protocol
C# Source Code: RunAppUrlProtocol.zip
Register a Custom URL Protocol Handler
Perhaps you're familiar with such URL protocol handlers such as http://microsoft.com, ftp://user:pass@server
outlook:Contacts
, file://http://www.coad.net/blog/images/Greenstone.bmp
,
相关文章推荐
- URL Protocol Handler
- 自定义URL Protocol Handler
- [转]自定义URL Protocol Handler
- 自定义URL Protocol Handler [转]
- Java魔法堂:URI、URL(含URL Protocol Handler)和URN
- Java魔法堂:URI、URL(含URL Protocol Handler)和URN
- 自定义URL Protocol Handler 呼出应用程序
- 自定义URL Protocol Handler
- 自定义URL Protocol Handler 呼出应用程序
- proxy: No protocol handler was valid for the URL /. If you are using a DSO version of mod_proxy, make sure the proxy sub
- URL Protocol Handler 的简单演示程序
- URL Protocol Handler demo
- URL Protocol Handler
- spring mvc基础篇(五):映射处理器之BeanNameUrlHandlerMapping
- org.springframework.web.servlet.handler.SimpleUrlHanderMapping异常处理
- HandlerMapping和BeanNameUrlHandlerMapping的使用
- Registering an Application to a URL Protocol
- Registering an Application to a URL Protocol
- Implements IConfigurationSectionHandler interface,create a custom configuration section handler
- web_url、web_custom_request、web_submit_data、web_submit_form的使用实例