积极答复者
自定义扩展名与WPF应用程序关联(文件关联)

问题
答案
-
你的App应该如下处理输入参数:
public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { string[] arguments = e.Args; //假设你的文件后缀是.ext if (arguments.Length > 0 && arguments[1].EndsWith(".ext")) { //得到被双击文件的路径 string filePath = arguments[1]; } } }
StartupEventArgs e 本身已经包含了传入参数
Bob Bao
Do you still use the same Windows 8 LockScreen always? Download Chameleon Win8 App quickly, that changes your LockScreen constantly.
你是否还在看着一成不变的Windows 8锁屏而烦恼,赶紧下载这个 百变锁屏 应用,让你的锁屏不断地变化起来。- 已标记为答案 Marry_qing 2015年12月11日 5:42
-
您好 kangxia,
>>这样还是不可以的
请问是您的文件没有关联上,还是传递到的WPF应用程序的文件路径没有取到。
如果是文件和您的应用程序没有关联上,请修改如下代码。文件需要的是您的WPF应用程序的文件名,而不是所在路径。所以您需要去掉GetDirectoryName方法,直接使用FileName即可。另外由于涉及注册表操作,所以您的这段代码要以管理员权限运行。
string appPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
修改为:
string appPath = Process.GetCurrentProcess().MainModule.FileName;
如果是文件路径在WPF应用程序中没有取到,请注意一下以下这行代码,您需要将.ext改成你的扩展名ico。
if (arguments.Length > 1 && arguments[1].EndsWith(".ico"))
Best Regards,
Jerry
- 已编辑 Jerry Wa 2015年12月11日 1:50
- 已标记为答案 Marry_qing 2015年12月11日 5:37
全部回复
-
您好 kangxia,
我们可以使用Environment.GetCommandLineArgs()方法来获取传递到应用程序中的参数,其中第二个参数就是被双击文件的文件名。 我们可以注册Application的Startup事件,然后在事件方法中获取文件路径并执行相应的操作。 以下示例代码供您参考。
public partial class App : Application { private void Application_Startup(object sender, StartupEventArgs e) { string[] arguments = Environment.GetCommandLineArgs(); //假设你的文件后缀是.ext if (arguments.Length > 1 && arguments[1].EndsWith(".ext")) { //得到被双击文件的路径 string filePath = arguments[1]; } } }
Best Regards,
Jerry -
Thank you very much!
我自己做了个小Demo,貌似没起作用,
string appPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
string extendName = ".ico"; //自定义的文件后缀名
string extendDefaultSet = "";
string directions = "";
RegistryKey key;
//1 设置自定义文件的双击打开
key = Registry.ClassesRoot.CreateSubKey(extendName);
key.SetValue("", directions);
key = key.CreateSubKey("shell");
key = key.CreateSubKey("open");
key = key.CreateSubKey("command");
key.SetValue("", string.Format("{0} %1", appPath));
key = Registry.ClassesRoot.CreateSubKey(extendName);
key.SetValue("", extendDefaultSet);
MessageBox.Show("关联创建成功!");public partial class App : Application { private void Application_Startup(object sender, StartupEventArgs e) { string[] arguments = Environment.GetCommandLineArgs(); //假设你的文件后缀是.ext if (arguments.Length > 1 && arguments[1].EndsWith(".ext")) { //得到被双击文件的路径 string filePath = arguments[1]; } } }
这样还是不可以的,是我上面的代码有问题吗?
-
你的App应该如下处理输入参数:
public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { string[] arguments = e.Args; //假设你的文件后缀是.ext if (arguments.Length > 0 && arguments[1].EndsWith(".ext")) { //得到被双击文件的路径 string filePath = arguments[1]; } } }
StartupEventArgs e 本身已经包含了传入参数
Bob Bao
Do you still use the same Windows 8 LockScreen always? Download Chameleon Win8 App quickly, that changes your LockScreen constantly.
你是否还在看着一成不变的Windows 8锁屏而烦恼,赶紧下载这个 百变锁屏 应用,让你的锁屏不断地变化起来。- 已标记为答案 Marry_qing 2015年12月11日 5:42
-
您好 kangxia,
>>这样还是不可以的
请问是您的文件没有关联上,还是传递到的WPF应用程序的文件路径没有取到。
如果是文件和您的应用程序没有关联上,请修改如下代码。文件需要的是您的WPF应用程序的文件名,而不是所在路径。所以您需要去掉GetDirectoryName方法,直接使用FileName即可。另外由于涉及注册表操作,所以您的这段代码要以管理员权限运行。
string appPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
修改为:
string appPath = Process.GetCurrentProcess().MainModule.FileName;
如果是文件路径在WPF应用程序中没有取到,请注意一下以下这行代码,您需要将.ext改成你的扩展名ico。
if (arguments.Length > 1 && arguments[1].EndsWith(".ico"))
Best Regards,
Jerry
- 已编辑 Jerry Wa 2015年12月11日 1:50
- 已标记为答案 Marry_qing 2015年12月11日 5:37