c# Project template with linked file

I exported a Project template from a working class library project, which has the following linked file element:

    <Compile Include="..\DalBase.cs">
      <Link>DalBase.cs</Link>
    </Compile>

This works fine in the original project, but when I create a project from my template the include path ends up pointing to some temp folder, such as:

C:\Documents and Settings\bradk\Local Settings\Temp\5oohnkvn.04c\DalBase.cs

The linked file is obviously broken then, and the project won't compile. Is there any way around this I tried a custom parameter with the relative path in it, and the same problem persists.



Answer this question

c# Project template with linked file

  • CRNaik

    A custom wizard would be the only way to work around this unfortunately.

    http://msdn2.microsoft.com/en-us/library/ms185301.aspx

    /Ole


  • Piotr K.

    That's not the case, if the file exists in the parent directory (i.e.: C:\Projects\MyFile.cs and the project is in C:\Projects\MyProject) then for some reason the csproj file ends up resolving this to some for of C:\Documents and Settings.

    If the linked file exists in a folder that is not a parent folder of the project file (i.e: C:\Temp\MyFile.cs) then it works but I think the behavior is wrong.

    Gabo


  • Krieg38

    That's what I ended up doing (adding the item through code), I haven't tried this with the recently released VS SP1, but that thing takes so long to install that I'm not sure if I will be recommending people to use it.

    Gabo


  • moonguy

    Gabo/anyone,

    Did you get anywhere with this As far as my own experiments are concerned this appears to be another bug in the extensibility model - if so, that sucks but is there a workaround in any way shape or form other than developers manually adding the links

    Like you I have a scenario where the file to be linked already exists in a relative parent folder. If I just specify the relative path to it in the .csproj file in the template zip, then the Wizard screws up the path by prefix a bunch more "..\" characters so the link is broken.

    So if I have this in my wizard template .csproj file (which is exactly what I want to end up with):

    <ItemGroup>
    <Compile Include="..\..\..\Configuration\AssemblyCommonInfo.cs">
    <Link>Properties\AssemblyCommonInfo.cs</Link>
    </Compile>
    ...

    Then after the wizard runs, it adds "..\..\..\..\" to the path, such that the folder effectively becomes "C:\Configuration\AssemblyCommonInfo.cs" which is nonsense.

    The directory created by the project wizard is in:
    C:\folder1\folder2\folder3\folder4\folder5\folder6\MyNewProjectFolderByWizard

    The linked file already exists at:
    C:\folder1\folder2\folder3\folder4\Configuration\AssemblyCommonInfo.cs

    Some form of resolution much appreciated - without it this is a major downgrade from the functionality in our VS.Net 2003 wizards.

    Thanks,
    Grant.


  • dwee

    Thanks. What would the wizard do that I am not doing now with a custom parameter Create a full path to the linked file
  • LiuCong

    Not exactly. The linked file already exists in the parent folder when these projects are created (this is a restriction on project location). The real problem is the template refuses to put a relative path in the linked file include. I just want the template to do exactly what I told it to do in the original project I exported: use a relative path, and don't translate it to a full path.
  • Calhoun Solutions

    Hmm, answering my own question - I did find a workaround which was mentioned earlier in the thread of doing it programmatically in the IWizard implementation class.

    For anyone else wondering on the details reading this later - I stripped out the above <Compile Include=".... snippet from the csproj file.

    Then in the IWizard.ProjectFinishedGenerating method, I added the following code (my linked file I want to be in the "Properties" subfolder of the project):

    ProjectItem propertiesProjectItem = project.ProjectItems.Item(1);
    propertiesProjectItem.ProjectItems.AddFromFile(ABSOLUTE_
    PATH_TO_LINKED_FILE);

    This correctly converts the absolute path to a relative one within the generated .csproj file - sorted.


  • raju44u

    Your problem is that the linked file is not copied to your project directory at all. The template wizard simply does not have support for linked items which is located outside the Project Cone. In a custom wizard you can write code that allow you to copy the file from the temporary to the requested direcory and then you can add a link to the file using ProjectItems.AddFromFile(pathToFile).
  • Anna Flynn

    If the linked file already exists in the parent directory of the newly created project I don't see any reason to have your vstemplate file know about that linked file. Just add the link in project file located in the zip file and your project should end up fine after it has been created.

    Ole


  • c# Project template with linked file