[英]C# File Association: passing double clicked file path to string
I recently made a notepad like program for C# and found a library for using file associations. 我最近为C#编写了一个类似记事本的程序,并找到了一个使用文件关联的库。 I am wondering how I would pass the path of the file double clicked in explorer, to a string so the file can read and 'open' the text file (like how notepad does).
我想知道如何将浏览器中双击文件的路径传递给字符串,以便文件可以读取并“打开”文本文件(就像记事本一样)。 I have googled for a while, and asked around a few forums, and my friends.
我用Google搜索了一段时间,并在几个论坛和我的朋友们周围问了一下。 Any answers or nudges in the right direction are appreciated.
任何正确方向的答案或推动都会受到赞赏。 Thank You
谢谢
(note: I've already tried reading it from the string[] args
paramater in Main()
, which was suggested by someone else) (注意:我已经尝试从
Main()
的string[] args
paramater中读取它,这是其他人建议的)
EDIT: Solved, it was the args[0]
. 编辑:解决了,它是
args[0]
。 I was really tired when I started on this 当我开始这个时,我真的很累
This works fine for me! 这对我来说很好!
public static void Main(string[] args){
if (args.Length == 0){
// Show your application usage or show an error box
return;
}
string file = args[0];
Application.Run(new MyProgram(file));
}
The suggestion was correct, the filename you double-click on in explorer will be visible in your app as an args parameter. 建议是正确的,您在浏览器中双击的文件名将在您的应用程序中显示为args参数。 Then you can do what you like with it, such as open a file.
然后你可以用它做你喜欢的事情,比如打开一个文件。
I've just made the following program 我刚刚制作了以下程序
When opening a file with this program, it tells me the path of the file. 使用此程序打开文件时,它会告诉我文件的路径。
class Program
{
static void Main(string[] args)
{
Console.WriteLine(args.Length);
foreach (string s in args)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
the output for me was 我的输出是
1
C:\Users\MyUserName\Documents\Visual Studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\New Text Document.txt
you might want to execute a similar program, by using file.openWith, to see what happens. 您可能希望通过使用file.openWith执行类似的程序,以查看会发生什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.