How can I use files in one project to create a library in another project in Visual Studio? - visual-studio

I have a standalone project that contains some code I'd like to re-used. I created a library project in order to contain that code. There does not appear to be a way to move files from the one project to the other. (cut/paste in solution explorer did not work)
I then manually copied the files in Windows explorer (outside of VS) into the library's main "folder" but they didn't show up in solution explorer either.
Anyone know of a way to do this without having to manually create each file in the library and then copying/pasting the code into the files?

You need to copy the files and paste them into your solution/project folder(s). Once the files physically exist within your solution/project folder structure (Windows FileSystem / Explorer), then you right-click on your project within Visual Studio, select Add then Existing Item. Browse to the location of that file and open it. It will now exist within your project as a code file.
If you have two projects within the same solution, you can simply drag the file from one project to the other within Visual Studio Solution Explorer. That will create a copy of the class in the new project. You can then remove the old one and clean up any code references in the first project.

Related

Adding Existing folder(s) to a C++ Project in Visual Studio without creating a copy

I have a Visual Studio Solution containing multiple projects. (One Main Project and rest are used for testing the Main project)
Here, i have a shared folder within the solution which is used by all the projects within the solution.
I need to include the entire shared folder in my Main project instead of adding existed items.
I am using Visual Studio Professional 2015 and i could not find "Add Existing folder" option in Visual Studio.
Adding Existing items to a project will eliminate the folder structure in solution explorer which makes the usage more complex when included file count is huge.
I need to maintain a folder structure within the project.
Moving the shared folder within the project directory and using "Show All Files" option will avoid the issue(tried based on the answers in some links) but it is not accurate in my case as the folder is being used by different projects.
Do i have any way to add existing folder (Outside the Project within the solution) to a project and maintain a folder structure so that the solution explorer looks more readable?
I just found the solution to this issue.
We are suppose to create a Filter (Virtual Solution Folder) within the Project. Option for creating Filter is available on following below steps,
Select Visual Studio Project.
In Solution Explorer, Disable "Show All Files" option.
Note: When "Show All Files" option is enabled, your project will have an option to create a Folder instead of Filter.
Create a Filter/Virtual Solution folder. (Name could be same as the existing folder name outside the VS project in order to maintain readability)
Now, add required existing files to the Virtual Folder Solution from any location.
(Inside/Outside project location)
By following the above steps, one can add set of existing files to a virtual folder structure in Visual Studio Solution. This is equivalent to adding an Existing folder to a Project Solution without copying it.

Create Visual Studio Solution from Batch File

Visual Studio seems to consist of a single solution file (*.sln) along with one or more project files (a C# project would have the *.csproj extension).
I have been playing around with a console application that parses existing directory entries to create solution files with the associated project files.
It works, but every time I run into a new project here at work I find myself spending a week or more debugging my console project so that it can churn out a solution for that particular work project.
Is there something out there already that can create a VS solution out of an existing file structure?
As you can tell from my screen capture below, these projects are nested very deep, so it would take a very long time to do this with the apps folder below with the "by mouse" technique in the Visual Studio IDE.
I created the custom console application that is posted in this post:
https://stackoverflow.com/a/22153536/153923
I invite others to contribute how they approached this solution, though.
So, I found out today that this feature already exists in Visual Studio.
Link 1: How to: Create a Project from Existing Code Files
Link 2: How to: Create a Project from Existing Code Files
Basically, though, it says this (just in case the MSDN links get changed or deleted):
You can create a Visual Studio project from an existing app—for example, an app that you obtained from an online source. Project and solution files are created on your computer and the other relevant files are added. A project can be created from Visual C++, Visual Basic, or Visual C# code files.
Security note Security Note
We recommend that you determine the trustworthiness of existing code files before you import them into Visual Studio, because Visual Studio will execute some of the code in a fully trusted process when you open the newly created project.
To create a project from existing code files
On the menu bar, choose File, New, Project From Existing Code.
The Create New Project from Existing Code Files wizard opens.
Use the wizard to specify the details of the existing code files that will be added to the project and the application that will be created when you build the project.
Another good answer was given by cbp in Visual Studio: Create a web application from existing code:
--
OK I figured it out. It's weird, but the following steps will work:
Open fresh copy of Visual Studio
File->New Project, select Web Application
Use the following settings:
Name: Website (this is the name of the existing folder with the website files in it)
Location: C:\Temp\ (anywhere will do for now)
Solution Name: TheProject (name of the existing project's root folder)
Check "Create directory for solution"
Delete the auto-created Default, Global and Web.config files
Save All and close Visual Studio
In Windows Explorer, copy the new folder on top of the existing folder so that the files are merged.
Double click on the sln file to open Visual Studio again.
Select "Show all files" (at the top of Solution Explorer)
Right click on any files or folders you want to add and select Include in Project.
Great idea!

Add reference deselected in visual studio 2010

I am trying to open a existing project in visual studio 2010 using
new->project from existing code
and selecting visual c# as the language. When I run it, I get errors. Later I heard from the person who created the project that I have to add few references, which are present in the references folder in the project itself. It comes in the side window - solution explorer.
However, when I right click on that folder to add as references, that option is 'greyed', or disabled. How Can I correct this?
The DLLs named freeglut.dll and glut32.dll are not COM or NET components. You cannot add them as references to your project (You have 4 solutions in your archive, I have choosen the one named fwa_annimate but I think the problem is the same).
The DLL named Tao.FreeGlut.dll needs these files in the same directory where you application run to work. So the easiest way to resolve your problem is to select the two DLLs inside Visual Studio and change the property Copy to Output Directory from Copy Never to Copy Always.
In this way, when you start to debug your app, the VS IDE will copy the two files to the BIN\DEBUG or BIN\RELEASE directory where you app runs when launched inside the VS IDE.
Of course, you need to deploy all these file when you distribute your application.
(By the way, I have no idea what is supposed to do)

adding multiple existing folders and multiple existing files to Visual Studio project in single step

Does anyone have a way to add multiple folders and (existing) files within those folders to a Visual Studio project in a single step (or via macro)? Let's assume all of the folders and files were copied/pasted into the project folder where they need to reside relative to the project folder, but are not actually stored in the .csproj or .vbproj file, which I believe is a requirement to compile those files into the solution/project.
This works great if you have a web application project in your solution, but not so well if you have a .csproj or .vbproj. In that case, you must manually add folders (and nested folders), and add existing files and browse to each one individually, which can take a long time.
In the solution explorer, select the project, click the "Show All Files" button - .
This will show all the files, even those not part of the project. You can now select all of these in the solution explorer (using Ctrl + Click) then right click and go to "Include In Project".
In Visual Studio 2013 (and possibly earlier) you drag the folder from windows explorer and drop it on the project in the solution explorer.
Other answer given in this thread wouldn't work for me as the files I want to add (DTSX files) are in a different folder, and copying/moving them causes other problems. Here is what worked for me:
Using Visual Studio 2015, use Windows Explorer to locate the files you want to add to your project. Copy those files to the clipboard. If DTSX files are what you are dealing with, in the Solution Explorer right-click on SSIS Packages, and then click on Paste.

VS 2010: Add as Link a complete directory of XML to a solution Folder? (no project exists at this level)

I need to add (as link) a complete directory structure that lives under my solution folder but not under any project folder.
Is this possible, i can't seem to find it.
I suppose 1 option is to add a new project type and then add these files under that project..
But it seems not a good way, what type of project type do i add, a c# class project? The files i wish to include are standard XML files and nothing to do with C#
Any ideas or i get around this?
This is possible in Visual Studio 2010 Professional and above. You can add a solution folder and then include files within that folder. Just right click the solution in the Solution Explorer and choose Add|New Solution Folder. Once the folder is added, right-click it and select Add|Existing Item... to add each of the files that you want to access from that folder.
Solution folders are symbolic (i.e. they don't have to map to a single physical location) so you can store shortcuts to files from many different file system locations under the same solution folder.

Resources