[RESOLVED] Include files as references, instead of a copy?
Hello,
I'm kind of new to C#, (and new to this forum),
But anyway, I used to be a vb6 / Java programmer, but recently I started to learn c#...
So, in vb6 and Java when you add a class, it just includes a reference to that file, which allowed me to make one folder with "includes", and use those files in many different projects.
In c# I noticed when I try to add an existing item, it creates a copy of that file, instead of using it as a reference, which is really bothering me.
I was wonder if its possible to include files in c# without actually copying the file into the project location.
Re: Include files as references, instead of a copy?
Yeah C# sucks about that. I have tried a million different ways to do this including putting the projects all in one folder (Don't do that by the way). For some reason MS just does not want to think that people would want different projects to share a file from a centralized location.
The only work around I came up with is to manually edit the .csproj file in notepad, It will do it correctly then but it will cause havoc with the source control, so you will have to manually check that file in and out of source control.
Anyway I will be watching, if anyone else has a solution I would be glad to hear it also.
--------------------------------------------------------------------------------------------------------------------------
Disclaimer - Most likely any code I have posted as an answer was most likely written free hand and may have some minor compile errors, and is merely intended to give you the idea.
Re: Include files as references, instead of a copy?
At first, you should share classes or assemblies respectivelly, not source files. Source files are not subjects of CLR, but of particular compiler. It is matter of architecture and maintability.
But if you really want to link files instead of copying them, yes, you can. In context menu of solution browser choose Add/Existing item and in the dialog opened look at Add button - there is small arrow on the right side. Click it and drop down menu will appeare where you can choose Add as Link.
But this is for VS 2005/2008. Althought I expect in VS2010 it is the same, I cannot proof it because I don't have it before me.
Re: Include files as references, instead of a copy?
A way of doing this would be to create DLL from your .cs file and add a reference to that. I use that quite often when using the same general methods in several projects.
Re: Include files as references, instead of a copy?
C# does allow this - it's just not well known (or well documented).
Here's how you do it:
1) Right click on the project in the Solution Explorer
2) Click on "Add\Existing Item"
3) In the "Add Existing Item" dialog, choose the file to add
4) Click on the down arrow next to the "Add" button
5) Select "Add As Link"
This will add the file as a link (without copying the file).
See the 1st screenshot for the Add options
See the 2nd screenshot for what a file looks like in the solution explorer after adding it as a link - notice the little arrow in the file icon.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.