从C#应用程序打开txt文件 [英] Open txt file from C# application

查看:1329
本文介绍了从C#应用程序打开txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码假设从我的C#应用​​程序打开CMD并打开文件text.txt。

The following code is suppose to open CMD from my C# application and open the file text.txt.

我试图将文件路径设置为环境变量,当记事本打开时,它会查找%file%.txt而不是text.txt

I tried to set the file path as an environment variable but when notepad opens it looks for %file%.txt instead of text.txt

任何想法为什么?

System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.EnableRaisingEvents=false;
        proc.StartInfo.EnvironmentVariables.Add("file", "c:\\text.txt");
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.FileName = "notepad";

        proc.StartInfo.Arguments="%file%";
        proc.Start();
        proc.WaitForExit();

        Console.WriteLine(proc.ExitCode);


推荐答案

如果你的目的是用a启动编辑器。 txt文件(就像问题的标题所说)只是使用:

If your purpose is to start the editor with a .txt file (like the title of the question says) just use:

Process.Start("C:\\text.txt")

这篇关于从C#应用程序打开txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆