Luke h
Luke h

Reputation: 337

Opening notepad file in web browser

Just wondering how I can open a text file in IE through a C# command? I can only so far make the file open, and then it asks to confirm which program to open it with. This seems a bit unprofessional and that is using this code:

Help.ShowHelp( button5,"file:E:/Gradecalculator/Gradecalculator/index.html");

or this code:

System.Diagnostics.Process.Start("file:E:/Gradecalculator/Gradecalculator/index.html");

My issue with these is that it asks the user to select a program to open it with, and I want it to automatically open it in IE (the user WILL have IE). Thanks if you can help!

Upvotes: 2

Views: 1084

Answers (2)

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

Problem : Files will be opened with their associated application by default, if no application is associated by default then it will ask for user to choose the application.

Solution : You need to Associate an Application with which you want to open your file.

Try This:

using System.Diagnostics;

Process process=new Process();
ProcessStartInfo start=new ProcessStartInfo();
start.FileName="IEXPLORE.exe";
start.Arguments=@"E:/Gradecalculator/Gradecalculator/index.html";
process.StartInfo=start;
process.Start();

Upvotes: 2

David Heffernan
David Heffernan

Reputation: 612854

The native answer to your direct question, is to execute iexplore.exe passing the URL as an argument. However, I will not recommend that, and so won't show you code. If you feel compelled to do that you can surely work out how to.

This code from your question:

Process.Start(@"E:\Gradecalculator\Gradecalculator\index.html");

is in fact the right way to do this. You should not force the user to use a specific web browser. You should respect their choice.

If this code leads to the user being asked which program to open the file with, then the issue is purely environmental. The file associations are broken on that machine.

So the right solution is to use the code above, and fix the associations.

Upvotes: 3

Related Questions