C# CF:如何使用我的程序打开特定文件扩展名

发布于 2024-08-24 22:40:55 字数 335 浏览 4 评论 0原文

我正在 .Net Compact Framework 中开发移动应用程序。 我设法编辑注册表 HKEY_CLASSES_ROOT,以便单击扩展名为 .xyz 的文件将打开我的应用程序。基本上,我需要在单击该文件时对其进行一些操作。

但是,我意识到,如果我第一次这样做,它会到达位于 static void Main 的 program.cs。但是,当程序运行时,我再次单击扩展名为 .xyz 的文件,它不会加载程序 static void Main。我尝试在当前正在运行的窗体上设置断点,但仍然没有任何结果。

那么它去哪里呢?如何检测文件 .xyz 被单击并执行某些操作?

I'm developing a mobile application in .Net Compact Framework.
I managed to edit the registry HKEY_CLASSES_ROOT so that a click on a file with the .xyz extension will open my application. Basically, I need to do some operation on this file when it's clicked.

However, I realise that if I do that the first time, it reaches program.cs at static void Main. But when the program is running and I click on the file with .xyz extension again, it doesn't load the program static void Main. I tried setting breakpoints at the form that is currently running but still nothing.

So where does it go to? How can I detect file .xyz is clicked and do something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

-小熊_ 2024-08-31 22:40:55

您遇到的大问题是,一旦应用程序运行,Main 将永远不会再次被调用,事实上也不应该。

不幸的是,在 Windows Mobile 下,CF 本身会尝试保持应用程序单例,因此当您尝试第二次运行应用程序时,CLR 本身会拦截该操作,并将现有实例带到前台。这样做的不幸的副作用之一是您没有机会处理命令行参数。

要使其正常工作,您必须执行以下操作:

  1. 颠覆 CF 使您的应用程序成为单一实例的尝试,以便您可以运行多个实例。
  2. 在应用程序中保留一个互斥体以了解您是否已经在运行。
  3. 尽早生成一个线程来侦听来自后续实例的传入参数。当参数进来时,把自己放在最前面并适当地处理参数。
  4. 启动时,如果互斥体已被占用(即应用程序已在运行),请使用 IPC(点对点队列是一个好方法)发送现有应用程序的侦听器线程(请参阅#3)您的参数,然后只需辞职。

The big problem you're having is that once the application is running Main will never get called again, and in fact it shouldn't.

Under Windows Mobile, unfortunately, the CF itself attempts to keep applications singletons, so when you try to runt he app a second time, the CLR itself intercepts that and instead brings the existing instance to the fore. One of the unfortunate side effects of this is that you get no opportunity to handle command-line parameters.

To get this to work, you have to do a few things:

  1. Subvert the CF's attempt to make your app a sinlgeton so you can run multiple instances.
  2. Hold a mutex in the app to know if you are already running.
  3. Spawn a thread early on to listen for incoming parameters from subsequent instances. When parameters come in, bring yourself to the fore and handle the parameters appropriately.
  4. On startup, if the mutex already is taken (i.e. the app is already running), use IPC (a point-to-point queue is a good way) to send the existing app's listener thread (see #3) your parameters and then just quit.
隱形的亼 2024-08-31 22:40:55

您说每次单击文件时都需要对文件执行一些操作。我假设这是与 GUI 相关的东西,就像您想在双击文件时显示文件的属性一样。

假设您的程序有一个主窗体,您可以将其 MinimizeBox 属性设置为 false 并在其 Deactivate 事件中放置 this.Close( );。这样,当您单击正确类型的文件时,您的应用程序将启动并读取命令行参数并显示文件详细信息。如果用户随后使用右上角的“确定”按钮关闭您的应用程序,它将真正关闭,以便下次打开时它将打开一个新实例并正确读取命令行参数。或者,如果用户只是导航到 WinMo 中的某个其他程序,则应用程序的 Deactivate 事件将触发,从而关闭该应用程序。无论哪种方式,应用程序始终要么打开并位于顶部,要么完全关闭,因此单击文件资源管理器中的文件将始终打开一个新实例。

You say that you need to perform some operation on a file every time it's clicked. I'm assuming this is something GUI-related, like you want to show properties of the file when it's double-clicked.

Assuming your program has one main form, you could set its MinimizeBox property to false and in its Deactivate event put this.Close();. This way, when you click a file of the right type, your application will start and read the command line args and display the file details. If the user then closes your app with the OK button in the upper right, it will close out for real so that the next time it opens it will open a new instance and read the command line args properly. Or, if the user just navigates to some other program in WinMo, your application's Deactivate event will fire, closing the app. Either way, the app is always either open and on top, or completely closed, so clicking a file in file explorer will always open a new instance.

偏闹i 2024-08-31 22:40:55

Main 中有字符串参数吗?

static void Main(string[] args)

Do you have string args in Main?

static void Main(string[] args)
辞取 2024-08-31 22:40:55

您的问题是您正在寻找再次调用 static void Main ,这是一个 静态构造函数。静态构造函数仅被调用一次。它们在类(在本例中是您的程序)初始化之前被调用,然后再也不会调用。程序的每个新实例都不会调用它们。

Your problem is that you're looking for static void Main to be called again, which is a Static Constructor. Static constructors are only called once. They are called before the class (in this case your program) is initialized, then never again. They're not called for each new instance of your program..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文