繁体   English   中英

从 Windows 资源管理器“打开方式”上下文菜单获取文件 C# Wpf

[英]Get file from Windows Explorer "open with" Context Menu C# Wpf

当用户选择在 c# 中使用 wpf 应用程序打开“.txt”文件并在文本框中显示 .txt 文件中的文本时,我如何从 Windows Explorer 获取 txt 文件?

这是我的意思是上下文菜单的图像。

我会在这里写下完整的答案,以便您接受。

解决办法是:

  1. 根据这个答案得到右键文件的路径。

  2. 使用System.IO.File.ReadAllText读取文件内容

  3. 通过设置其 Text 属性显示文本框中的文本。

在代码中,这将用于 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
        {

        }


    }

和正常的文本框

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM