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 am working on a .NET Core/C# solution that contains multiple projects. I have done so before but for this one I set up everything through a remote server in a linux terminal. Once all my projects were created and pushed to GitHub, I pulled to my local machine. Problem I am running into is that intellisense is not working at all in any project in my solution. I am wondering if I set up everything correctly or if there is something I am missing. Below is some screenshots of my directory and one screenshot of VS.
root dir from repo
1 dir down into BlabberApp folder
2nd dir down into BlabberApp.DomainTest
What is shown in VS when I open root dir
What is shown in VS when I open solution file
I have tried opening using open file for: .csproj file, .cs file, as well as open folder for various folders that hold my separate projects. I also tried to open folder to the BlabberApp folder which contains the folders that hold my .sln as well as the folders that hold all my separate projects. I need intellisense to recognize references some projects have with each other. I also need it to recognize my tests to properly test everything. One thing peculiar I found is when I open the .sln file it shows in the solution explorer that 0 projects are loaded. Also none of my tests are recognized in the test explorer (I have tried cleaning and building the sln with no changes showing in test explorer).
I have also gone through the steps of going to tools -> options -> text editor -> c# -> intellisense to check/uncheck the boxes under completion lists. Any advice would be helpful! Thanks
It seems any of your projects are not listed in your solution. Somehow they unloaded from the solution. You can add the projects one by one in the solution. Or you can take help of a plugin that will add all projects from the solution folder at once. please add them by the following step:
Add this VS extension
Right-click on the solution and click on Add multiple projects
check Add solution folder
Click Load Projects From Folder
Select the solution directory
Select all projects
Click start
please follow the link instructions for more details.
Project name is testProject. I want to create a new folder named "TestProject" and move the project into it, because I want to add testProject.uTest and testProject.iTest for unit and integration tests. So everything related is located in the same folder.
The solution is in VSTS. Already one folder exists with the same name because of the project. Namespaces will all change.
How do I go about this the best way?
I got everything to work.
First I opened the solution and deleted the project in there. Then I created the new folder in the solution explorer. I then copied all the project related files into the new folder in windows explorer. I opened up the solution and added this existing project and add all files to source contrl. Next I had to re-add nuget packages and fix some references in other projects. Next I pushed the changes and then I deleted the old project files in source control explorer.
Took 10mins and everything works fine!
A few simple steps that worked for me:
Create a folder and add some text/number at the end:
Example: Infrastructure1
Create/Add the Project to your solution without the text/number at the end.
Example: Infrastructure
Rename the folder (Infrastructure1) to match your Project name(Infrastructure).
That's it, and you will get rid of the message that Project can't be named with the same name as the folder.
Is there a way to build solution into a single folder? I have several projects in this solution and each access a config file that should be in the current directory. I just move each project's build files into one and it still works, however, it looks so unorganized and messy. I just want to know it there are other ways on how to do it.
You can set output directory in the settings of every project in solution (if we are about Visual Studio). Menu: Project -> properties -> Build -> Output path. For example, I use ..\Build\ to build projects into Build directory of solution root.
This MSDN article explains how to do it in a nice, DRY way:
https://blogs.msdn.microsoft.com/kirillosenkov/2015/04/04/using-a-common-intermediate-and-output-directory-for-your-solution/
It allows you to specify those directories only once, and use those settings in multiple projects.
Steps:
Create a single common.props file in solution, that will specify and overwrite output and intermediate paths for a project to a common directory (like "Solution/bin").
Here is a sample *.props file that I found linked in the article:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<SolutionDir>$(MSBuildThisFileDirectory)</SolutionDir>
<Configuration Condition="$(Configuration) == ''">Debug</Configuration>
<OutputPath>$(SolutionDir)\bin\$(Configuration)\</OutputPath>
<OutDir>$(OutputPath)</OutDir>
<OutDir>$(OutputPath)</OutDir>
<IntermediateOutputPath>
$(SolutionDir)\obj\$(Configuration)\$(MSBuildProjectName)\
</IntermediateOutputPath>
<UseCommonOutputDirectory>False</UseCommonOutputDirectory>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
</Project>
Include this file into every *.csproj that you want to set the common output dirs for, by adding this line (the actual path may differ): <Import Project="..\Common.props" />
You can change projects "Output path", by default it's bin directory of given project.
Right click on each project, select Properties from context menu, then select Build tab.
Ont the bottom in Output section change Output path:. Set same path for each project.
I agree with comments under your question, you should not change it. Instead you may create post build action (PS script) that will copy all files from project's bin directories to one designated by you.
Update:
Set this script as Post Build command (Project's properties->Build Events tab->Post build event command line):
xcopy "$(TargetDir)*" "$(SolutionDir)Build" /s /i /Y
For each project:
Go into the project properties, in the "Build" tab.
Choose "All configurations", "all platforms", just in-case.
In the output folder write "..\bin\" (or any path which is uniform for all of them - not in the current project directory). Alternatively, to organize DLLs in sub-folders you can write "..\bin\Sub-project-directory" in the output path. Then you should add an App.config file for the EXE project with a probing to all DLLs so they can be found and loaded on runtime.
Note that if we're talking about building multiple executables into the same output directory, you can also add them as project references to the main (startup) project. They will be automatically copied to the main project output directory everytime you build it.
(note: this applies to .NET Core projects in VS 2017 or VS 2019. I'm not sure if it would work for .NET Framework projects)
In your opinion , it would be possible to change a file full path in a C# project, In Visual Studio 2012?? I ask this because in visualization, the properties box for a project file that contains full path appears as disable ( gray).
I would use the same file in different project, under the same solution, without duplicate file code .cs.
To expand on #TheSolution's answer:
File > New Project (or Solution > Right-click > Add New Project) > Class Library -- name it something like "MyProject.Common"
Add Existing Files (Shift+Alt+A) to project > choose files to share. Or copy/paste
Delete old files in original project, as they've been copied to the shared project
Then in the projects in which you want to use the common files, Project > Add Reference > Choose from under "Solution > Projects" in left panel, check the "Common" project and choose "OK"
Include the namespace in the using section at the top of the code files which use the common code.
If you're talking about renaming a C# project and its folder, you have to do this manually in the .sln and .csproj files after renaming the project in the solution. ex) If you want to rename "ME.MyProject" to "ME.YourProject":
Project > Properties (ALT+ENTER on project in Solution Explorer)
Change both "Assembly name" and "Default namespace"
Close VS (or unload solution)
Rename folder
Open .sln file in a text-editor and follow the manual steps below
Open the .csproj files of all other projects referencing this one and change references from "MyProject" to "YourProject". Alternatively, delete and re-add project references within VS.
.sln:
BEFORE:
Note that the project file has been renamed, but not the path.
Project("{SOMEGUID}") = "ME.YourProject", "ME.MyProject\ME.YourProject.csproj", "{ANOTHERGUID}"
EndProject
AFTER:
Project("{SOMEGUID}") = "ME.YourProject", "ME.YourProject\ME.YourProject.csproj", "{ANOTHERGUID}"
EndProject
.csproj:
BEFORE:
<ProjectReference Include="..\ME.MyProject\ME.YourProject.csproj">
<Project>{ANOTHERGUID}</Project>
<Name>ME.YourProject</Name>
</ProjectReference>
AFTER:
<ProjectReference Include="..\ME.YourProject\ME.YourProject.csproj">
<Project>{ANOTHERGUID}</Project>
<Name>ME.YourProject</Name>
</ProjectReference>
If you wanted to share a code file then you could add it as a Linked File. You can do this by right clicking on the project, selecting add existing item, finding the item in the open file dialog, and then before selecting it drop down the Open button and select the option for linked.
This will now link that code file to this project - or in other words - when it's changed in one place it's changed in both.
However, linked files are a very hacky way of providing shared functionality. The more appropriate approach is to build a Class Library with the functionality and add the DLL as a reference to the project. This means that you'll need to have a way of keeping that DLL up to date - but it's still generally more appropriate.
I have created a ClickOnce Solution with VS2008.
My main project references another project who references COM dll as "links".
When I build my solution in VS the dlls from the orther projects are moved in my bin folder but when I publish and launch the project these files are not presents in my Local Settings\Apps\2.0... folder.
I know that I can add each dll of the other project as a reference of my main project but I'd like a cleaner solution ...
Is it possible ?
First add those files to your project directly.
Then goto Application properties -> Publish -> Application files
Select "show all files" if you do not see the files you need and then set their
publish status to "Include" NOT "Include (Auto)". This is important or they will not be added.
Please note if you update the files, you will have to remove them and add them again
and set their publish Status again. This is a small bug.
See a previous question of mine for more info:
ClickOnce - Overwriting content files
You need to open the "Application Files" dialog in the Publish tab of your project. From there you can set the publish type (Include, Prerequisite, etc.) of each of your files.
If it's an unmanaged DLL, you'll need to add the actual .dll as a file to your project and mark its build action as "Data". You can then set the Publish Type of that file to Include.
I had the same issue.... and the only way to fix this after going through many options, was by adding those dlls to References.
It works, but I hope there would be a cleaner solution to it in future.