[英]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.