CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2010
    Posts
    4

    [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.


    FYI, I'm using Visual Studio 2010.

    Thanks in advance for the help

  2. #2

    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.
    Last edited by matthewsanford@gmail.com; July 1st, 2010 at 03:18 PM.
    --------------------------------------------------------------------------------------------------------------------------
    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.

  3. #3
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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.
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  4. #4
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    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.
    It's not a bug, it's a feature!

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.


    __________________________________________________________
    Arjay

    See my latest series on using WCF to communicate between a Windows Service and WPF task bar application.
    Tray Notify - Part I Tray Notify - Part II

    Need a little help with Win32 thread synchronization? Check out the following CG articles and posts:
    Sharing a thread safe std::queue between threads w/progress bar updating
    Simple Thread: Part I Simple Thread: Part II
    Win32 Thread Synchronization, Part I: Overview Win32 Thread Synchronization, Part 2: Helper Classes

    www.iridyn.com



    ...
    Attached Images Attached Images   

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Include files as references, instead of a copy?

    Oops. I missed that Boudino has already answered it.

  7. #7
    Join Date
    Jul 2010
    Posts
    4

    Re: [RESOLVED] Include files as references, instead of a copy?

    @boudino, oke thanks

    @arjay, yea, thanks to boudino I've found the "add as link" option. Its pretty well hidden.. lol

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: [RESOLVED] Include files as references, instead of a copy?

    Quote Originally Posted by Necrowizard View Post
    Its pretty well hidden.. lol
    I'd say it's very well hidden.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured