Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS2013 - How to pass the solution folder, $(SolutionDir), in the command line arguments for a C# project using an external program

I am building a C# adding for Excel. In order to debug it, I need to launch Excel.exe with a command line argument containing the Debug or Release path to the addin.

For example:

  • Start External Program: C:\Program Files\Microsoft Office\Office15\EXCEL.EXE
  • Command line argument "C:\Dev\Project1\Project1\bin\Debug\Project1-AddIn64.xll"

However, I would like to replace "C:\Dev\Project1\Project1\bin\Debug" with an equivalent of $(SolutionDir) for C++ projects in VS. Is there a way to do this ? If it is not doable, is there a way to get around this ?

EDIT: please support me and get this added in VS by voting up the following idea: http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/6350738-support-for-macros-in-debugging-command-line-argum

like image 994
BlueTrin Avatar asked Aug 25 '14 18:08

BlueTrin


People also ask

What is $( SolutionDir?

$(SolutionDir) The directory of the solution (defined as drive + path); includes the trailing backslash '\'.

How do you pass command line arguments in Visual Studio C++?

To set command-line arguments in Visual Studio, right click on the project name, then go to Properties. In the Properties Pane, go to "Debugging", and in this pane is a line for "Command-line arguments." Add the values you would like to use on this line. They will be passed to the program via the argv array.

How do I change the target path in Visual Studio?

You can open *. csproject file with any text or XML editor, change the wrong path, then save the file. Then re-open the project with Visual Studio again.


3 Answers

Indeed, the macros cannot be used in the Start Options | Command line arguments

I see two solutions:

  1. As the current folder is set to the $(TargetDir) when you Start an application you could refer to the solution folder like this: ..\..\..\ if the External program accepts a relative path. (I am not quite sure why you would ever want to refer to the solution folder, referring to the output/target folder makes more sense to me)

  2. In the Post Build event (unregister) and register the component the way the component should be registered when deploying it (a proper setup). This way you only have to refer to Excel in the Start Action. This also immediately adds the benefit of testing a scenario that is more similar to production.

like image 95
Emond Avatar answered Oct 25 '22 15:10

Emond


It's not exactly a fix, but this may help some people. If you create your project from the project template "Visual C#/.NET Core/Console App" instead of "Visual C#/Windows/Console App", this feature is supported. When I put "$(SolutionDir)" in the Application Arguments field on the Debug tab of the Project Properties window, it is expanded at run time. Note that you will need Visual Studio 2015 Update 3 or later.

like image 44
Gyromite Avatar answered Oct 25 '22 15:10

Gyromite


I guess you could make use of post-build event to read in your file. @HansPassant explained it in VS2010 - Project Macro Variables in Start Options Command Line Arguments.

A short quote:

A possible workaround is a post-build event that writes a file that you read in your program. Like echo $(ProjectName) > "$(TargetDir)cmdargs.txt

You could substitute cmdargs.txt to appropriate file you want.

like image 21
nevets Avatar answered Oct 25 '22 14:10

nevets