[英]Get file from Windows Explorer "open with" Context Menu C# Wpf
How i can get the txt file from the Windows Explorer when the user chooses to open a ".txt" file with the wpf application in c# and display the Text from the.txt file in a Textbox?当用户选择在 c# 中使用 wpf 应用程序打开“.txt”文件并在文本框中显示 .txt 文件中的文本时,我如何从 Windows Explorer 获取 txt 文件?
I'll write the complete answer here so you can accept it.我会在这里写下完整的答案,以便您接受。
The solution is:解决办法是:
Get the path of the right-clicked file based on this answer .根据这个答案得到右键文件的路径。
Read the contents of the file using System.IO.File.ReadAllText使用System.IO.File.ReadAllText读取文件内容
Display the text in the textbox by setting its Text property.通过设置其 Text 属性显示文本框中的文本。
In Code this would be for richtextbox在代码中,这将用于 richtextbox
public MainWindow()
{
InitializeComponent();
try
{
string[] args = Environment.GetCommandLineArgs();
richtextbox.Document.Blocks.Clear();
richtextbox.Document.Blocks.Add(new Paragraph(new Run(File.ReadAllText(args[1]))));
}
catch
{
}
}
and for nomral textbox和正常的文本框
public MainWindow()
{
InitializeComponent();
try
{
string[] args = Environment.GetCommandLineArgs();
textbox.Text = File.ReadAllText(File.ReadAllText(args[1]));
}
catch
{
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.