【问题标题】:Get file from Windows Explorer "open with" Context Menu C# Wpf从 Windows 资源管理器“打开方式”上下文菜单 C# Wpf 获取文件
【发布时间】:2020-11-15 13:17:32
【问题描述】:

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

【问题讨论】:

标签: c# wpf visual-studio contextmenu windows-explorer


【解决方案1】:

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

解决办法是:

  1. 根据this answer获取右键文件的路径。

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

  3. 通过设置文本属性在文本框中显示文本。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-17
      • 2011-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      相关资源
      最近更新 更多