Add folders to a c# projects using dotnet cli commands - c#

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.

Related

Share global using alias between multiple projects in C# [duplicate]

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

Creating a project for web assets in Visual Studio 2013

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.

Visual Studio: different project folder structure for publishing/editing than the physical one

I'd like to add files to a web csproj that are located under its directory, but in a different directory structure than the physical one. Example physical directory structure:
MyProject.csproj
Assets
Settings.config
Now my project needs the following structure when I publish the project (i.e. on the server when deployed) and would be nice if this structure would be visible from Visual Studio too:
MyProject.csproj
Config
Settings.config
Notice that Settings.config isn't in the Assets but in the Config folder.
I don't want to copy the files over, I can't move them and I can't change where these files are loaded from.
Is it possible for such files to be located in the Assets folder while
from VS they appear to be in the Config folder,
and when the project is published they are copied to the Config folder.
The latter one is possible with post-build tasks I think and I believe I can implement it. However I'd like to have the whole development experience, including the folder structure in VS, to show the file being under Config. Is this possible?
I'm looking for something like solution folders, but for projects. Adding files as links would work great, but since the files are in the physical folder of the project I get the error "Cannot add a link to a file that is inside the project tree".

Shared resources in a vsix template project

I created a solution with following structure
Solution
Installer (VSIX)
Template One (sdk project)
One.vstemplate
Template Two (sdk project)
Shared
AssemblyInfo.cs
Now I want to link the AssemblyInfo.cs in the shared project to my vstemplates, because i want the same AssemblyInfo.cs in all my projects.
I dont want to have sevaral copies because of possible future developments.
Till now i tried to add the AssemblyInfo.cs as link, but the template build process still search in the project folder and does not find the file (Add as link).
In addition i tried to reference the file using "\..\Shared", then i got a error out of root path.
One way I does not try is to use prebuild commands to copy the AssemblyInfo.cs to each project folder.
Has anybody an idea, tipp or solution for this situation?
I get a working version by using pre-build commands to copy the files from shared to the single templates.
Any other ideas how to solve this request?
Sorry for the late response.
I tried to reference the AssemblyInfo by adding following code to the vstemplate file
<ProjectItem ReplaceParameters="true" TargetFileName="Properties\AssemblyInfo.cs">..\Shared\AssemblyInfo.cs</ProjectItem>
My current solution is to add to each project a pre build command
copy /Y "$(ProjectDir)..\Shared\AssemblyInfo.cs" "$(ProjectDir)AssemblyInfo.cs"

Which files in a Visual C# Studio project don't need to be versioned?

I'm new to Visual C# Studio (actually using the Express edition, but another developer is using the full version), and we are using version control (svn).
It's acceptable to me to add the project files to the repository, since this repo is just for the two of us using Visual C# Studio. But it feels like there are some irrelevant files in there. Can someone familiar with Visual C# Studio project files tell me which files can safely be svn:ignored?
Some suspect files:
project.csproj
project.csproj.Debug.cachefile
project.csproj.user
project.sln
project.suo
Content\Content.contentproj
I'm sure the .sln is required, but what's this .suo? and the .csproj? Can/should any of these be generated by Visual C# Studio on loading a project?
Dont include
bin
obj
*.suo
*.user
_Resharper* (if you have Resharper)
Include
*.sln
*.csproj
You can also check the .gitignore file for visual studio projects on github.
.csproj defines the project structure. It is vital.
This is what I add to my global ignore list in Tortoise SVN:
*.suo *.user bin obj *.pdb *.cache *_svn *.svn *.suo *.user *.build-res TestResults _ReSharper*
Shouldn't be versioned:
.csproj.user is the user's project file settings (e.g. startup project)
.suo is the user's solution file settings
Should be versioned:
.sln is the solution file itself (what projects it contains etc)
.csproj is the project file
I'm not sure about "contentproj" but it sounds like it's a project file which should be under svn.
The .sln file defines your solution together with the .proj files (one for each project), so keep them in your svn!
You can skip the .suo file (personal settings - binary anyway) as well as the bin or obj folders. Also the .cache files can be left.
Just to add, anything that gets regenerated at build time, should be excluded. For example, files generated from the prebuild event or in some cases a custom tool.
Needed...
*.sln - The solution file contains references to all the projects and dependencies between the projects.
*.csproj - The project files themselves. These tell what files are included in the project, references, and the build steps for the project.
Not...
*.suo - This is a user settings file...
You definitely need csproj files... You might want to try AnkhSVN or VisualSVN, those VS addins add only the required files to SVN.
Or you could remove files from your directory structure until it does not load anymore.
I suggest experimenting this way because it is a great way to learn how a solution is structures by VS.
I leave out the Visual Studio Solution User Options file (*.suo) and the binaries directories as they get recompiled everytime you build your solution (the bin and obj folders).
As this as not been indicated in other answers yet :
In the case you are using Visual Studio with Unity 3D, you can safely add both *.csproj and *.sln to the .gitignore file, on the contrary of the usual case.
Indeed, the project structure is managed by Unity itself, not by Visual Studio. The only consequences of keeping them in source control are conflicts, even more so if for some reasons different Visual Studio versions are being used among commiters.
Example .gitignore for Unity 3D : https://github.com/github/gitignore/blob/master/Unity.gitignore
We also work with Visual Studio C# and SVN and I don't know about all the project files, but we only exclude the complete bin directory.
Check this out - type visualstudio amd you will have a .gitignore file generated for you, also you can concatenate multiple languages/ide' ignore files together if you have a solution which contains multiple languages.

Categories

Resources