I try to reference a collection of c# code to a console project in visual studio.
I want to keep the referenced code outside the console project folder.
I want the code to automatically update when it gets changed in the external position.
The referenced codes folder structure should stay intact. (~100 files)
Is there a way of referencing/linking the code without updating everytime? "Include in Project" only works if the code is inside a solution folder.
You can add a "link" to code files outside of your project. This doesn't make a copy of the files.
Right-click your project -> Add -> Existing Item..., and browse to the file(s). Then click the down-arrow on the "Add" button and select "Add As Link":
Linked files appear with a blue arrow in the Solution Explorer:
If you want to reference an entire folder structure of code, you'll need to edit your .csproj. Something like:
<ItemGroup>
<Compile Include="..\SomeDir\**\*.cs" Link="%(RecursiveDir)%(Filename)%(Extension)"/>
</ItemGroup>
Adjust ..\SomeDir to be the path to your code. The **\*.cs is of course a pattern to recursively include all .cs files. %(RecursiveDir), %(Filename), and %(Extension) are MSBuild placeholders.
I organise my git repos in a flat structure:
core
shared1
website1
website2
Where websites 1 and 2 both reference core and shared1.
When I add project references to core and shared1 from websites 1 and 2, they remain intact because they are both in the same relative location from the point of view of the websites.
Additionally, I organise my solution to mirror the external repo configuration, e.g.
core (solution folder)
corelibrary.csproj
shared1 (solution folder)
sharedlibrary.csproj
website1services.csproj
website1.csproj
I found a good solution, but I am curious if there is a better one. I am on Windows and using a simbolic link
cmd /c mklink /d /j _LinkedCode ..\..\..\_Code
then I can use "Include in Project" in visual studio
Related
I know this can be done via Visual Studio. But I wanted to know if there is a command or a workaround to add folders to a project.
My projects are: Class Libraries a project for creating a class library that targets .NET or .NET Standard
I tried with a shell script:
mkdir DTOs;
mkdir Behavior;
mkdir Features;
mkdir Filters;
mkdir Interfaces;
mkdir Mappings;
mkdir Wrappers;
folders are created but they are not added to project.
Thanks in advance.
Please check out the following detailed answer by poke.
Paraphrasing it for reference here:
Project folders
In modern .NET Core projects (using the .NET SDK), files are automatically added based on a global file pattern. For example, any .cs file anywhere within your project directory is by default automatically configured to be a part of your project that needs to be compiled. This pattern however only applies to files, not directories.
Directories are not an explicit part of a project by default. Instead, they are only there if they are “needed” for a path to a file. That’s why you won’t see folders within Visual Studio until there is a file that is part of the project.
If you are within your project folder and then add a folder there, you will not see the folder there. But as soon as you add a file to that folder (echo '' > TempFolder\Test.cs), it should automatically be picked up by Visual Studio:
You can also enable the “Show all files“ option in the solution explorer, to make folders that are not part of the project appear in the solution folder:
As you can see, the folder appears as a transparent item because it is not part of the project itself. You can then right click on the item and choose “Include In Project“ to make this folder an explicit part of the project. This action will add the following section to the project file:
<ItemGroup>
<Folder Include="TempFolder\" />
</ItemGroup>
This basically tells Visual Studio that the folder is part of the project even though it does not contain any files. As soon as you do add any file to the folder, Visual Studio will remove that configuration though since the folder is now again an implicit part of the project.
Solution folders
Visual Studio solutions don’t show the actual directories within your solution directory but rather a virtual directory as configured within the .sln file. Projects being located within subdirectories will not automatically be located within such a folder within the solution structure, and similarly non-project folders will also need to be added to the solution file first.
There is no mechanism to manage the solution folders with the dotnet sln command though. The only thing that you can do is add a project into a particular virtual folder within the solution:
dotnet sln add path/to/project.csproj --solution-folder VirtualFolder
This would add the project.csproj inside the VirtualFolder solution folder within the Visual Studio solution.
If you want to manage the solution folders otherwise, you should do that with Visual Studio.
I have the file Pathes.cs(in the screenshot);
The file is not in any project. The other projects does not see it. My task is to do that file visible for all projects.
I tryed to use using Homework, to use Homework.Pathes.pathToDataDirectory in my projects.
But nothing helps.
You have a few options:
Create a Class Library project, put the file in there and reference that project in all other projects that need that class. Right-click on Dependencies in the Solution Explorer, go to Projects and select the shared class library project. This is the preferred option.
Link the file to the projects that need the file. Right-click on the project, click Add > Existing Item and navigate to the file. Instead of clicking the Add button, click on the arrow next to it and select Add As Link.
Copy-paste the file into the projects. This is a total maintenance nightmare, so don't go there.
Not sure how to describe my question and ok here it goes:
We already have some existing projects created and under TFS source control.
A typical project is then stored locally as:
SomeDir\SomeNameSpace.SomeName\ProjectName\
then the csproj and sln file inside that directory will be named: SomeNameSpace.SomeName.ProjectName
Now when I want to create that same setup using Visual Studio it goes horribly wrong:
I have tried all possible options on creating a project and either it creates an extra SomeNameSpace.SomeName.ProjectName folder extra under the ProjectName directory and puts the sln and csproj inside that dir or I can create a Project under the proper ProjectName dir only then the name of the sln and csproj files will be ProjectName.snl/csproj which is neither what I want.
Renaming them of course would be the logical thing to do, however, doing that causes the sln to go corrupt. When I try to reopen it will throw a Save as DevEnv.sln in my face and that is not what I want.
Edit: Of course I have renamed inside the sln and csproj file which neither led me to the solution
So how can I create a new VS2015 solution that will nicely sit under
\SomeDir\SomeNamespace.SomeName\ProjectName\ with
SomeNameSpace.SomeName.ProjectName.sln and
SomeNameSpace.SomeName.ProjectName.csproj as it's name?
What am I missing, that it seems impossible to achive this in a simple manner?
Create the Project from the New Project VS Window. The settings you need are:
Name: ProjectName
Do NOT create directory for solution
This should create the csproj and sln files in the appropriate directory.
From here, right clic on the project in Solution Explorer -> Rename -> SomeNameSpace.SomeName.ProjectName
This way, the solution reference will be updated too.
Now, rename the solution too, from Solution Explorer.
File menu, Save All. This saves both the project AND the solution.
And I think that's it.
You should in your new project window have it look like this
Please note the highlighted aria is unticked
Ok we have figured it out.
The solution as posted by A. Chiesa was almost right.
The solution, projects and namespaces need to be set.
- Rename the Solution within the "Solution Explorer" for example name it "NameSpace.SomeName.ProjectName"
- Rename the Project within the "Solution Explorer" for example also name it "NameSpace.SomeName.ProjectName"
- Go to the project properties and rename within Application the "Assembly name:" for example also name it "NameSpace.SomeName.ProjectName"
- Go to the project properties and rename within Application the "Default namespace:" for example also name it "NameSpace.SomeName.ProjectName"
Then if you are lucky, you can use Resharper to rename the namespace for all files throughout your project or you have to do it manually (depending on files count can be a very tedious job to do).
That should do it.
Thanks everyone for your input and advice.
Best regard,
Ed
I have multiple ASP.NET MVC5 projects that use several similar styles/scripts, and I think, for maintenance's sake, it'd be ideal to have a separate project in Visual Studio 2013 containing the LESS stylesheets and JavaScript files that can be shared between the two.
My setup is as follows:
Web.Project1
Web.Project2
Web.Assets //would contain LESS/JS files
Resources
Domain
Data
Any pointers?
I've been looking at adding as a link, but I'm not certain that would accomplish what I need.
I recently went through this process for a project of mine, and concluded that if your goal is simply to avoid duplication by keeping your code DRY then using linked files works great.
It's simple enough to do, but I'll write it up anyway as it might save someone some time.
Quick Summary
Create common Solution Folders containing the resources to be shared, or simply designate one of the projects to be the master.
Use "Add as Link" to add the shared resource files to each project as needed.
Add an AfterBuild task to each project file that will copy the linked files to project folders. This is only needed so that Visual Studio test/debug (F5) will work locally.
Detailed explanation follows:
Configuring Solution Folders for the Shared Resources
** Note that if you're simply going to share files directly from one project to one or more additional projects then you can skip this step.
Visual Studio solution folders do not have to reflect physical file system folders, but doing so will help preserve your sanity. So first create the folders on your local file system and copy the resource files into them. The new folders should be located under your solution folder. For example:
\MySolution
\Common
\Images
\Scripts
\Styles
Back in Visual Studio, right click on the Solution Items folder and use Add Solution Folder to replicate the new file system folders.
Next, add the files to the new solution folders by right-clicking each folder and using Add Existing Item to add the contents of the folders.
Add Shared Resources as Links
For each project that will use the shared resources, right-click the project folder and choose Add Existing Item. Browse to the common folder, select the desired files, click the drop-down arrow next to the "Add" button and choose "Add as Link".
You may get a source control warning about adding files that are outside of the project directory structure, but this can be ignored since the linked file will be under source control at its source.
Add an AfterBuild Task to Copy Files
When you publish the application to a server the linked files will copied to the project folders to which they are linked and everything works as expected. However, in the development environment the linked files do not physically reside in the project folders. So when you hit F5 to test your application in VS, the shared resources will be missing.
The simple solution is to add an MSBuild task to copy the linked files from their source after each build. This needs to be done to for each project that contains the shared resource links.
Right-click the project and choose Unload Project. Right-click the project again and choose Edit <ProjectFileName>. Scroll to the bottom and add the following (just above "</Project>"):
<Target Name="AfterBuild">
<!-- Copy linked content files to local folders so that they are available in the debugger.
This is only an issue when running the application locally. The linked files should
be automatically published to the correct folder when publishing to a web server. -->
<Copy SourceFiles="%(Content.Identity)"
DestinationFiles="%(Content.Link)"
SkipUnchangedFiles='true'
OverwriteReadOnlyFiles='true'
Condition="'%(Content.Link)' != ''" />
</Target>
** Copy task adapted from this link.
Save the project file then right-click and choose Reload Project.
I have two projects. One where I can see the properties folder within the project and the other I can't. I have right-clicked on the solution and selected properties, but I see no way to add the folder.
I have tried (within Windows), going to the folder of my project and adding the folder that way, but I still need a way to associate the project properties with that folder I have added.
I've seen this thread. It says the folder is created by default when you create a project and if I delete them, I'll have to recreate it. I've never seen the folder on any of my projects, except one and I didn't delete it from the others, so not sure.
I attached a picture of what I am referring to in my project that actually has it if that would help. Thanks.
You need not just add folder in windows explorer, but also include it in Project (with 'Show all files' option, find this folder under project and 'Include in project' from rigth click menu).
OR, just create it from within VS (right click on Project -> Add -> New folder, and name it 'Properties')