In a C# project I try to use relative paths for "Start external program" and for "Working directory".
I tried relative paths starting with ../
and relative paths with $(SolutionDir)
/ $(ProjectDir)
With all tries I get an error popup. (The external program cannot be found / the working directory you entered does not exist) - see screenshots.
Is it possible to use relative paths and how? I also searched on msdn but there is almost no info about the csproj.user file.
We need this as we don't like to force a folder structure for all developers.
This is stored in the csproj.user file (myproject.csproj.user) like this:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ProjectFiles</ProjectView>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<StartAction>Program</StartAction>
<StartWorkingDirectory>%24%28SolutionDir%29\..\..\..\..\mydir</StartWorkingDirectory>
<StartProgram>%24%28SolutionDir%29\..\..\dir\myapplication.exe</StartProgram>
</PropertyGroup>
</Project>
File paths are a common stumbling block for novice programmers. First, what’s the difference between a relative file path and an absolute file path? A relative path describes the location of a file relative to the current (working) directory*. An absolute path describes the location from the root directory.
Relative paths are resolved relative to the current working directory. When you’re running a Windows Service, the default working directory is C:Windowssystem32 or C:WindowsSysWOW64. Therefore relative paths are resolved from these system folders, which can lead to problems when read/writing files.
You can add the config directory with the definition.py file to any project so you have a very nice, generalized solution for handling relative paths. Using a single file that contains a single variable we simplify calculating paths a bunch! In addition this trick is very simple an can be easily copied into every project.
A relative path describes the location of a file relative to the current (working) directory*. An absolute path describes the location from the root directory. When learning to access data files through programming, we regularly use relative file paths. In these Java examples, we define a path to the EQs_last_last_week_of_2021.csv file.
Edit directly in the .csproj file without escaping the characters, like this:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ProjectFiles</ProjectView>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<StartAction>Program</StartAction>
<StartWorkingDirectory>$(SolutionDir)..\..\..\..\mydir</StartWorkingDirectory>
<StartProgram>$(SolutionDir)..\..\dir\myapplication.exe</StartProgram>
</PropertyGroup>
</Project>
Also there is no need for the slash after $(SolutionDir)
I know this might sound the same as others, I just want to be clear you did exactly this.
You should
enter this
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectView>ProjectFiles</ProjectView>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<StartAction>Program</StartAction>
<StartWorkingDirectory>$(SolutionDir)..\..\..\..\mydir</StartWorkingDirectory>
<StartProgram>$(SolutionDir)..\..\dir\myapplication.exe</StartProgram>
</PropertyGroup>
</Project>
Close notepad
Try to use tags without condition, like
<PropertyGroup>
<RunWorkingDirectory>$(MSBuildProjectDirectory)</RunWorkingDirectory>
</PropertyGroup>
Also, consider using the $(MSBuildProjectDirectory) variable, that is filled by default, when parameters are passed via cli, e.g. dotnet run -p testmvc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With