简体   繁体   English

C#:如何处理/解析应用程序的消息,例如拖放相关的文件类型?

[英]C#: How do you handle/parse messages for your applications like drag and dropping an associated file type?

C#: How do you handle/parse messages for your applications like drag and dropping an associated file type? C#:如何处理/解析应用程序的消息,例如拖放相关的文件类型?

Let's say I have a text document application and I want it to execute and open a file if, 假设我有一个文本文档应用程序,我希望它执行并打开一个文件,如果,

1.) a .txt document is dragged on top of the exe. 1.)将.txt文档拖到exe的顶部。

How could I make that possible, to execute the text software, and two, finally open and display the text document in the text document software? 我怎么能做到这一点,执行文本软件,两个,最后打开并在文本文档软件中显示文本文件?

When someone drags-and-drops a text file on your application's executable, your app will get started and the path of the text file will be passed as a parameter. 当有人在应用程序的可执行文件上拖放文本文件时,您的应用程序将启动,文本文件的路径将作为参数传递。 You should be able to examine it in your Main method. 您应该能够在Main方法中检查它。

You have to find where your Main method is declared to be sure its signature includes the args parameter, then you can check upon args array and you will find the complete pathname of the file dragged on your application's exe. 您必须找到声明Main方法的位置以确保其签名包含args参数,然后您可以检查args数组,并且您将找到在应用程序的exe上拖动的文件的完整路径名。 Now you can then work with it accordingly to your needs. 现在,您可以根据需要使用它。

Example: 例:

static void Main(String[] args)
{
    string p = args[0];

    string e = Path.GetExtension(p);
    if (e == ".txt")
    {
        // It's a text file
    }
}

You can also drag more than a file and find their names inside the same array. 您还可以拖动多个文件,并在同一个数组中找到它们的名称。 Remember that in my example i don't check if there are actually some elements in args array and thus i can get an IndexOutOfBoundException if nothing is dragged (or passed as argument) when launching the application and finally that using Path.GetExtension method doesn't assure you the file is what you think, but just it has that extension. 请记住,在我的示例中,我不检查args数组中是否实际存在某些元素,因此如果在启动应用程序时没有任何东西被拖动(或作为参数传递),那么我可以获得IndexOutOfBoundException,最后使用Path.GetExtension方法没有确保你的文件是你的想法,但它只有它的扩展名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您如何在C#中检查当前与应用程序相关的进程? - How do you check a current applications associated process in C#? 将文件拖放到 wpf/C# 应用程序时,如何在 Windows 资源管理器中维护文件顺序? - How do I maintain the file order in Windows Explorer when drag and dropping files into wpf/C# application? 如何保护C#应用程序访问的SQL服务器 - How do you secure your sql servers accessed by C# applications 如何通过C#代码处理Date字段中的NULL? - How do you handle NULLs in a Date field through your C# code? 当c#中没有扩展名时,如何检查文件类型? - How do you check a file type when there is no extension in c# 在站点中使用C#查询外部IP时,如何处理抛出超时错误的现有代码? - How do you handle existing code throwing a timeout error when querying a site for your external IP in C#? 你如何称呼这种二进制数据类型,以及如何在 C# 中处理它? - How do you call this binary data type, and how to handle it in C#? 您如何发布或部署您的 C# 桌面应用程序? - How do you publish or deploy your C# desktop application? C#:我想像文件路径一样将消息传递给我的表单应用程序,比如控制台应用程序,我该怎么做? - C#: I want to pass messages like a file path to my forms application like a console application, how would I do that? 如何处理C#中C DLL文件中的复杂结构返回类型? - How do I handle a complex struct return type from a C DLL file within C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM