Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

c# - Visual Studio Linked Files don't exist

In Visual Studio, you can do Add -> Existing Item and then Add as Link from the Add drop down button.

This is great. This let's you add a file from another project, and editing the file also edits it in the original project.

I would like to use this feature to have a config file (named Shared.config) be present in all projects within one solution. And have that file always be the same.

solution
|
|- project 1
|- Shared.config [physical]
|- project 2
|- Shared.config [linked]

After publishing, the file indeed ends up in all published projects, so no problem there.

But BEFORE publishing (during development on build) the linked file doesn't really exist. Trying to see the file in the Windows Explorer proves that the file is not in the project directory. Visual Studio only makes it look as if it exists there in the solution explorer. (Though on build, linked items are probably copied to the bin directory; But I don't want to use/access files from the bin directory.)

Now this gives problems off course. Trying to print out System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("Shared.config")) will fail before the project has been published, because of the fact that Shared.config doesn't exist in the project root directory yet.

What I would like to do, and where I need your help is:

  • I would like to ON BUILD copy all linked files from their original location to their target location.

This will make visual studio have the linked file, and a copy of the original, to exists both in the same directory with the same name.

Normally , VS won't allow you to create a linked item in a directory if that directory already contains a file with the same name.

But, I have tested by creating a linked item first; then using Windows Explorer to copy the original file to the destination directory, and see Visual Studio act ok. The solution explorer simply hides the physical file, and shows the linked item in stead. (Even if you click Show all files in the solution explorer.)

solution
|
|- project 1
|- Shared.config [physical]
|- project 2
|- Shared.config [linked]
|- Shared.config [physical, copied here during build, invisible to Solution explorer]

This is exactly what I want! When trying to edit the file, Visual Studio will open the 'linked item'. And on build, a physical file will be copied to the target directory so it exists for the code that tries to access it.

Now how do I do this? Should this be done with Build events? If so how do I say 'copy originals of all linked files to their destination directory?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I had some troubles with dss539's answer (tested in vs2010). First, duplicates appeared in solution explorer for every matched file. Second, '%link' is not a property accessor, it's string itself and it always not equals to empty string '', so every file with None build action was matched, duplicated and copied on build. Third, linked files are copying into project's root directory but I needed files to be copied where links are placed. So I made some modifications:

<Target Name="CopyLinkedFiles" BeforeTargets="Build">
  <ItemGroup>
    <LinkedItem Include="@(Content)" Condition="%(Content.Link) != ''" />
  </ItemGroup>
  <Copy SourceFiles="@(LinkedItem)" DestinationFiles="%(LinkedItem.Link)"/> 
</Target>

LinkedItem is no longer duplicates items in solution explorer. To make link to be copied you have to set Content build action.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...