0

I'm trying to solve a problem i got. My job is to make little app, that will show text which is inside of .txt file in the app window, but for some reason they told me that i have to use @ ShellExecute(use Process.Start). Is there even a way to do it? Because when i use ShellExecute, that file opens in notepad after button press, which is, I guess, point of using Shell.

There is little code of what i tried to do, but without success. Thanks in advice!

string filePath = @"C:\Folder\file.txt";
ProcessStartInfo psi = new ProcessStartInfo(filePath);
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = true;
var proc = Process.Start(psi);
string s = proc.StandardOutput.ReadToEnd();
textBox1.Text = s;
  • 1
    I guess the best place to start is to ask this question to whoever gave you the job. So that you know that you are on the same page and there is no miscommunication going on. – Sergey Krusch Jun 22 '19 at 18:21
  • Yeah, wanted to do the same, but it is one of the tasks i need to do as job interview app, so I wanted to do my best to solve it if possible before i contact them about it. – Milos Lalic Jun 22 '19 at 18:53

2 Answers2

0

If you absolutely need to do that, you can create a second application TxtToTextBox, which you can run from your first application using Process.Start (initialize ProcessStartInfo with the path to that application instead of the txt file).

Then you can give that process an argument pointing to the file using psi.Arguments = $"\"{filePath}\"; (this also adds quotation marks around your path, so spaces are escaped).

Then in your second application you can do the sane thing, and simply read the file with File.ReadAllLines(args[0]) and print that into your text box.

If possible, I would recommend talking to whoever told you to use Process.Start and asking them for more reasons as to why you should use is, as this is one of the most roundabout ways to do this I could think of.

blenderfreaky
  • 738
  • 7
  • 26
  • Yeah, ReadAllText do it in one line, but this is one of the tasks i need to do as job interview app. Will try to contact them to see do I really need to use that. Thanks for the help! – Milos Lalic Jun 22 '19 at 18:51
  • Was googling a bit, and find that its possible to run like notepad directly inside app using a panel. How could I implement that here? – Milos Lalic Jun 22 '19 at 21:01
  • Take a look at [this](https://stackoverflow.com/questions/758494/how-can-i-run-another-application-within-a-panel-of-my-c-sharp-program), it seems to directly solve your problem – blenderfreaky Jun 22 '19 at 21:16
  • 1
    Yeah, was doing it for 3h, and after that i solve a problem. Notepad was not visible if app is not in full screen. But finally, we did it, hehe. Thanks! :) – Milos Lalic Jun 22 '19 at 21:30
0

Instead of using ProcessStartInfo, try StreamReader like this :

        string filePath = @"C:\Folder\file.txt";
        StreamReader sr = new StreamReader(filePath);
        string s = sr.ReadToEndAsync().GetAwaiter().GetResult();
        Console.WriteLine(s);

Use Async method to read all text without blocking.

Fatihi Youssef
  • 411
  • 1
  • 6
  • 15