简体   繁体   English

单击按钮后如何显示.txt

[英]How to make a .txt appear after clicking a button

I asked this question a minute ago and was not specific enough so let me try again. 我在一分钟前问了这个问题,但还不够具体,所以让我再试一次。

I am trying to generate a report of inventory information that is already made and have it update from the user input into text boxes on the form and then have a button to make the .txt file of the report show to the screen and have the updated information on it. 我正在尝试生成一个已经完成的清单信息报告,并使其从用户输入更新到表单上的文本框中,然后具有一个按钮来使报告的.txt文件显示在屏幕上并进行更新。信息。

I have the GUI created and have the button created and the .txt file is created. 我创建了GUI,创建了按钮,并创建了.txt文件。 I just need to know how to make it where I can click the button and have the .txt file appear to the screen. 我只需要知道如何制作它,就可以单击按钮并使.txt文件显示在屏幕上。

    Using System.Diagnostics;
    ... 
    String filename = "C:\\....\data.txt"; \\ File Created With Information
    Process.Start(filename); \\ Will open file with default program

The above code can be used to open an external program to display your text file. 上面的代码可用于打开外部程序以显示您的文本文件。

As usual I recommend using try/catch since you are dealing with external I/O (files). 与往常一样,由于您要处理外部I / O(文件),因此我建议您使用try / catch。

You can just start the notepad process with your *.txt file as the argument and start the process can't you? 您可以使用* .txt文件作为参数来启动记事本过程,对吗?

Found this link that might help you: http://www.csharp-station.com/HowTo/ProcessStart.aspx 找到了此链接可能会对您有所帮助: http : //www.csharp-station.com/HowTo/ProcessStart.aspx

  • Assign a click event to your button (in your class constructor for instance): click事件分配给您的按钮(例如,在类构造函数中):

     button.Click += new EventHandler(button_Click); 
  • In the event, start notepad.exe in a new process: 如果发生这种情况,请在新过程中启动notepad.exe

     void button_Click(Object sender, EventArgs e) { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "notepad.exe"; startInfo.Arguments = "C:\\Path\\To\\My\\file.txt"; Process.Start(startInfo); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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