views:

136

answers:

1

I'm playing about with an addin to Visual Studio 2005 that calls an external process.

When I run the code outside of the addin - i.e. in a standalone project it works fine. However when I call it as part of a addin the Process.Start() call is made but then nothing happens, the subsequent lines of code are never reached.

I have tried running VS with standard and elevated priviliges but get the same effect.

The code is below - it is called when clicking on a custom menu item:

        string documentPath = @"C:\TestCode\TestApp\Testform.cs";
        string folder = Path.GetDirectoryName(@"C:\TestCode\TestApp\");

        System.Diagnostics.Process p = new System.Diagnostics.Process();
        p.StartInfo.FileName = "notepad.exe";
        p.StartInfo.Arguments = documentPath;
        p.StartInfo.UseShellExecute = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.Start();

        string output = p.StandardOutput.ReadToEnd();

I've tried different executables, but this does not make any difference. Am I going about this the wrong way in VS? Any help is appreciated.

+1  A: 

Have you tried try/catch? In particular there are a number of gotchas relating to the working path / current directory with VS extensions (but I would expect notepad to work, at least).

I'm also not sure what you expect that code to do (in terms of redirecting stdout of notepad.exe); can you clarify?

Not an issue at the moment, but note that when working with paths as arguments, you'll want to add quotes from the start - i.e.

p.StartInfo.Arguments = "\"" + documentPath + "\"";

(in case the path has spaces in it)

Marc Gravell
Doh! I think I got all mixed between all the vaious issues I had and did not set UseShellExecute to false which is required for redirecting the output. Setting this to false fixed it and the process started. I'm accepting this answer because putting a try/catch around it helped me discover the error. I was mistakenly thinking that if an exception was raised then it would be shown in the IDE, but it seems not when debugging an addin....
When all else fails... exception handling ;-p
Marc Gravell