locked
Relative path for references RRS feed

  • Question

  • I have a C# project (using visual studio express C#).
    I'm using source control application to stored the project.
    The problem is, the references inside the project using absolute path.
    for instance, the project stored in:
    c:/LocalCustomFolder/project/MySharedProject/
    and the references keep in:
    c:/LocalCustomFolder/project/dlls/

    the path of the references that I add to csproj will be absolute. If I use "MyDll.dll" inside "dlls" folder so the path will be:
    c:/LocalCustomFolder/project/dlls/MyDll.dll

    And if someone get the project from source control, the path will be different, and the references path will be wrong.
    I want to change the path to relative so it will always valid, like "../dlls/MyDll.dll", not "c:/LocalCustomFolder/project/dlls/MyDll.dll"

    is it possible?

    Thanks

    Edit:
    I think I make a wrong statements.
    So if I get from source control the path will be empty, not absolute path.
    But the strange part is, when I only copy the whole project's folder, the path will refer to the new folder name.
    What actually happen?

    More Edit:
    I found out if I copy "<project-name>.csproj.user" file, the problem solved.
    I open the file, it doesn't contain any information whatsoever. How this file help the csproj find the path?

    Monday, May 5, 2008 1:16 PM

Answers

  • CM and VS base paths relative to the solution directory.  All paths within a solution are relative to the solution root.  If you reference any files (source, assemblies, etc) outside the solution root then absolute paths are used.  This is by design.

    VS generates a warning when you don't follow this rule and you try to add the solution to CM.  You probably ignored the warning as it can be somewhat confusing.

     

    You need to reset your solution root such that it contains all the files you want to reference.  If you don't do this then you are stuck with absolute paths.  I generally recommend the following layout:

     

    <Solution>\mysolution.sln

    <Solution>\Assemblies - third-party assemblies that I intend to reference but have no source for

    <Solution>\Source\Proj1

    <Solution>\Source\Proj2

    ...

    <Solution>\Tests\Proj1

    ...

     

    The .user file contains your per-machine, per-user settings for a project.  The solution-level settings are stored in .suo.  You don't store these files into CM as they are specific to a user and machine.  They contain all your user-level settings such as debug options, startup project, opened documents, etc.

     

    Michael Taylor - 5/6/08

    http://p3net.mvps.org

     

    Tuesday, May 6, 2008 3:00 PM
    Moderator

All replies

  • CM and VS base paths relative to the solution directory.  All paths within a solution are relative to the solution root.  If you reference any files (source, assemblies, etc) outside the solution root then absolute paths are used.  This is by design.

    VS generates a warning when you don't follow this rule and you try to add the solution to CM.  You probably ignored the warning as it can be somewhat confusing.

     

    You need to reset your solution root such that it contains all the files you want to reference.  If you don't do this then you are stuck with absolute paths.  I generally recommend the following layout:

     

    <Solution>\mysolution.sln

    <Solution>\Assemblies - third-party assemblies that I intend to reference but have no source for

    <Solution>\Source\Proj1

    <Solution>\Source\Proj2

    ...

    <Solution>\Tests\Proj1

    ...

     

    The .user file contains your per-machine, per-user settings for a project.  The solution-level settings are stored in .suo.  You don't store these files into CM as they are specific to a user and machine.  They contain all your user-level settings such as debug options, startup project, opened documents, etc.

     

    Michael Taylor - 5/6/08

    http://p3net.mvps.org

     

    Tuesday, May 6, 2008 3:00 PM
    Moderator
  •  Gani Budi wrote:
    I have a C# project (using visual studio express C#).
    I'm using source control application to stored the project.
    The problem is, the references inside the project using absolute path.
    for instance, the project stored in:
    c:/LocalCustomFolder/project/MySharedProject/
    and the references keep in:
    c:/LocalCustomFolder/project/dlls/

    Thanks

     

    i have a few projects that contain tens of thousands of lines of soruce code. Files are read and written. I found a solution to your problem long ago by defining a static variable cur_drive where all my source code and references are located. I define this variable in a class called Globals under the namespace of the project or if the solution consists of a few namespaces (projects) then it is outside of the namespaces.

     

    public class Globals

    {

    private static string cur_drive_ = String.Empty;

    public static string cur_drive

    {

    get { return cur_drive_; }

    set { cur_drive_ = value; }

    }

     

    }

     

    In the event delegate:

    private void Form1_Load ( object sender, EventArgs e )

    {

    Globals.cur_drive = Application.StartupPath.Substring ( 0, 2 );  // this value may be "C:" or whatnot

    .......

    }

     

    in Console apps I do it in Main ( )

     

    Furthermore all references for all paths are made this way:

    if ( File.Exists ( Globals.cur_drive + @"\VFP_Projects\DATA\ComeAndGetDatBase\logFile.txt" ) == false )

    {

    // do something

}

 

I have hundreds such places in the source code. I have transfered my apps from one HDD to another and no problem arose.

 

Now, I am thinking about storing some data (massive chunks) on external media. In this case, i will get in trouble, unless something clever will be thought. That other drive cannot be fererenced this way for obvious reason but it is something in the future.

 

Wednesday, May 7, 2008 2:29 AM
  • Thanks. but Taylor Michael's answer is more suitable for my case.
    But that's a good trick, I'll use it for other things.
    Thanks again.
    Wednesday, May 7, 2008 2:34 AM