7

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?

Flynn1179
  • 11,925
  • 6
  • 38
  • 74
  • What program are you hoping to "Edit" with, and what is the file type? – samuelesque Jun 11 '14 at 15:37
  • That's my point- I don't know. Whatever the user would get if they tried to edit it themselves. I can't assume knowledge of what's on the user's PC, and Notepad is unlikely to be appropriate in every case. – Flynn1179 Jun 11 '14 at 15:39
  • Good call on the duplicate- I'm not working with images specifically, but the solution's valid anyway. – Flynn1179 Jun 11 '14 at 15:43
  • @Flynn1179 In the question, you should state what type of files you need this to work with. I know you don't know ahead of time: so state that! It's important to provide all relevant information in the question instead of assuming everyone knows what you're talking about. – mason Jun 11 '14 at 15:55
  • With all due respect, I'd have to disagree. You can't specify every condition that ISN'T there, otherwise I'd have to start mentioning that I need an answer that works on any .net platform, any OS, any locale (just in case file extensions are handled differently), etc.. It was a general question, and the answer was general as well. There really is no reason to consider or assume specific cases. Generality should always be assumed, unless a specific case is highly likely and makes a significant difference to the answer. – Flynn1179 Jun 11 '14 at 19:15

1 Answers1

9

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");
}
Flynn1179
  • 11,925
  • 6
  • 38
  • 74
samuelesque
  • 1,165
  • 13
  • 30
  • I think a try/catch here, with the regular "Open" procedure in the catch, would cover things. – Joel Coehoorn Jun 11 '14 at 15:49
  • Yep, you could go that route as well. – samuelesque Jun 11 '14 at 15:51
  • Yeah, that's what I did. I have to admit though, I prefer not relying on catching exceptions if there's an alternative. Only because visual studio whines at me when I'm debugging. – Flynn1179 Jun 11 '14 at 15:52
  • Check out my updated answer, and let me know if the method to see if it has the edit verb works, I can't test it from here. – samuelesque Jun 11 '14 at 15:53
  • 1
    Works fine, except "edit" is lower case :) Personally, I prefer `string.Equals` over `==`, to allow adding globalization options where necessary, but that's not relevant in this case. – Flynn1179 Jun 11 '14 at 16:05
  • Bah, I knew I was going to screw something up! Glad it's working for you now. Cheers. – samuelesque Jun 11 '14 at 16:06
  • I have the confession that anyone who uses `string.Equals` in C# comes from Java ... – kevin Jun 11 '14 at 16:07
  • Not I. It's just easier to add in things like `CultureInfo.OrdinalIgnoreCase` as a parameter rather than muck around making both sides upper case or anything like that if I want the comparison case insensitive. – Flynn1179 Jun 11 '14 at 16:09