Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open a file for editing from code [duplicate]

Tags:

c#

edit

How do you open a file for editing from C# code, i.e. equivalent to the user right-clicking on it and selecting 'Edit' instead of 'Open'? I've got Process.Start(<filename>) for the equivalent of 'Open', but I specifically need the 'Edit' option in this case.

If there isn't an easier way of doing it, I'm assuming I probably need to inspect the registry for the 'Edit' action associated with the file type and invoke that action somehow, but I'm not sure where to look or how to do so reliably.

Anyone know the best way of doing this?

like image 386
Flynn1179 Avatar asked Jun 11 '14 15:06

Flynn1179


1 Answers

Not all extensions have the Edit ProcessStartInfo.Verb, but the following may help you in some cases.

var runFile = new ProcessStartInfo(pathToFile) {Verb = "edit"};
Process.Start(runFile);

If you want to check to see if the Edit verb is valid before starting the related process, you could try the following:

public bool HasEdit()
{
     var startInfo = new ProcessStartInfo(pathToFile);
     return startInfo.Verbs.Any(verb => verb == "edit");
}
like image 148
samuelesque Avatar answered Sep 30 '22 17:09

samuelesque