Search code examples
.netvisual-studio-2010visual-studio-2008ideprojects-and-solutions

Automatically move source code files to project folder in Visual Studio 2008/2010 which are added as link and residing outside the project folder


Is there a plugin/trick/macro for visual studio which automatically moves all files present outside a project directory and added as link, to the project directory and alters the location of the file in the project itself?

|--Project1
          |--File1.vb REM: already inside the project folder
|--File2.vb REM: outside the project folder and added as link to project1.

Is there any way to accomplish this?

|--Project1
          |--File1.vb REM: remains as it is
          |--File2.vb REM: now moved to project1 directory.

Solution

  • It should not be too hard to write such a tool by yourself. The project files are just XML files, and references to source files look like this:

    With links:

    <ItemGroup>
      <Compile Include="..\Path_To_External_File\SourceFile.vb">
         <Link>SourceFile.vb</Link>
      </Compile>
    </ItemGroup>
    

    Same without links:

    <ItemGroup>
        <Compile Include="SourceFile.vb" />
    </ItemGroup>
    

    So write a very simple program or script which reads all the project files to be processed, loop over all <Compile> items, make a copy of the source file or just move it to the desired location, and adapt the XML of the project file accordingly. If you have ever programmed some XML processing beforehand, using, for example, theXMLDocument class, then this should be implemented in a few hours (at maximum).

    One final remark: you may (or may not) have to to deal with the case where the same files is referenced from different projects.