使我的应用程序成为打开所有 .txt 文件的默认应用程序 [英] Making my application to be the default one to open all .txt files
问题描述
发布我的应用程序后,我可以指定我的应用程序 exe 文件作为打开 .txt 文件的默认文件.这里如何获取调用应用程序的文件的文件路径?
After publishing my application, i can assign my app exe file to be the default one to open .txt files. Here how can i get the filePath for the file which have invoked the application?
public MainWindow()
{
InitializeComponent();
string filePath = "";
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
txt.Text = sr.ReadToEnd();
sr.Close();
fs.Close();
}
这里如何在用户从资源管理器双击某个 txt 文件时获取文件路径..?
Here how can i get the filePath when user double click on to some txt file from explorer..?
推荐答案
在 .NET 中有两种方法可以获取命令行参数.您可以将参数列表放在 Main
方法上,也可以使用 Environment.GetCommandLineArgs
方法.
There are two ways to get the command-line arguments in .NET. You can put a parameter list on your Main
method, or you can use the Environment.GetCommandLineArgs
method.
var allArgs = Environment.GetCommandLineArgs();
// The first element is the path to the EXE. Skip over it to get the actual arguments.
var userSpecifiedArguments = allArgs.Skip(1);
由于您使用的是 WPF(因此不控制 Main
方法),您最好的选择是使用 GetCommandLineArgs
.
Since you're using WPF (and therefore don't control the Main
method), your best bet would be to go with GetCommandLineArgs
.
这篇关于使我的应用程序成为打开所有 .txt 文件的默认应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!