首页 > 解决方案 > C#打开文本文件,如双击

问题描述

在.net框架中,当我需要像双击行为一样打开文本文件时,我使用了以下代码

System.Diagnostics.Process.Start(path);

当我尝试在核心中使用时出现此错误

System.ComponentModel.Win32Exception:“指定的可执行文件不是此 OS 平台的有效应用程序。”

此异常最初是在此调用堆栈中引发的: System.Diagnostics.Process.StartWithCreateProcess(System.Diagnostics.ProcessStartInfo) System.Diagnostics.Process.Start() System.Diagnostics.Process.Start(System.Diagnostics.ProcessStartInfo) System.Diagnostics .Process.Start(string) HandelString.cs 中的 AccountManager.Setting.HandelString.Encrypt(string) Main.cs 中的 AccountManager.Main.Main_Load(object, System.EventArgs) System.Windows.Forms.Form.OnLoad(System.EventArgs ) System.Windows.Forms.Form.OnCreateControl() System.Windows.Forms.Control.CreateControl(bool) System.Windows.Forms.Control.CreateControl() ... [调用堆栈被截断]

错误信息在这里

我阅读了很多问题并尝试了解决方案,但不适合我。

标签: c#winforms

解决方案


您可以通过执行以下操作使用其默认应用程序(在 Windows 中设置)打开文件:

Process fileopener = new Process();
fileopener.StartInfo.FileName = "explorer";
fileopener.StartInfo.Arguments = "\"" + pathToFile + "\"";
fileopener.Start();

推荐阅读