Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use relative paths for working directory & start in C# project

In a C# project I try to use relative paths for "Start external program" and for "Working directory".

enter image description here

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.

enter image description here

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>
like image 865
Julian Avatar asked Aug 14 '19 08:08

Julian


People also ask

What is a relative file path?

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.

What is relative path in Windows service manager?

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.

How to handle relative paths in a project?

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.

What is the difference between relative and absolute 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. 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.


3 Answers

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)

enter image description here

like image 77
Lev Avatar answered Oct 19 '22 18:10

Lev


I know this might sound the same as others, I just want to be clear you did exactly this.

You should

  1. close visual studio - it's important as it might override your file if you keep it open
  2. open myproject.csproj.user using notepad.
  3. 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>
    
  4. Close notepad

  5. Open Visual Studio
  6. Go to project properties and confim you see this enter image description here
like image 36
Adam Marczak Avatar answered Oct 19 '22 20:10

Adam Marczak


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

like image 1
valerysntx Avatar answered Oct 19 '22 20:10

valerysntx