Visual Studio: Assembly name and Default namespace from project name - visual-studio

Context: setting up the Assembly Name/Default namespace in the Project Properties (right click on project, Properties, Application tab; or edit the .csproj).
I would like to add a parameter there to explicitly have them following the Project Name, plus some custom suffix. So if I rename my project, the Assembly name/namespace follow that rename.
Example: project named foo → foo_suffix.dll. If I rename the project to foo1 → foo1_suffix.dll.
Same behaviour for the namespace.
Reading Common macros for MSBuild commands and properties,
I tried inputting $(ProjectName) both in VS and in the .csproj file, but I get a .dll named exactly like that.
How can I get this done?

How can I get this done?
1. Try using $(MSBuildProjectName) property from this document.
You're developing a C# project(csproj) while your document above is about macros in C++ projects(vcxproj). So I think $(MSBuildProjectName) property can be more suitable for you after my several tests.
(I tried $(ProjectName), but it only worked well when I set something like <AssemblyName>$(ProjectName)</AssemblyName>, if I add a suffix like $(ProjectName)_suffix, the output sometimes is _suffix.dll. But it always work well if I use $(MSBuildProjectName)!)
I get the desired behaviour, but that is inconsistent with what I can
see in project, Properties, Application tab.
2. Try reloading the project file(Right-click project, unload.., reload...) to Update the UI.
From your comment, it seems like the $(ProjectName)_suffix works well in your machine? If so, you can still use $(ProjectName) property.
What you experienced is expected behavior for Msbuild and VS. VS will read value from csproj during project loading process. Then it display the related value by UI.
So after we rename the project name and then build the project, we can get a ProjectName_suffix.dll in output folder. But the UI is still not changed(inconsistent), cause now we need to Update the UI.
You can right-click project node, unload the project=>reload the project to get UI updated. And now the output xx.dll can be consistent with what you see in project, Properties, Application tab. It needs us to reload the project each time after you rename the project, then the UI would be consistent with your xx.dll. And actually it won't affect your build though the UI is not updated, the output can be your desired behaviour.
3. All above is more related to the relationship between Assembly Name and Project Name. One point you should know is msbuild(csproj) won't modify the source files(xx.cs) after the project is created.
So assuming you have a Class1.cs file whose namespace is Library. If you rename the project to NewLibrary, reload the project and you can see the Namespace is NewLibrary now in project, Properties, Application tab. But the namespace for Class1.cs file is still old Library. This is expected behavior in VS, and the new NameSpace NewLibrary would work if you create a new xx.cs file.
Hope all above helps :)

Related

If i want to make a copy of my c# project in the same workspace how can i do it?

In other words lets say i have already completed a project using visual studio. Now i want to make a separate project that is identical to the already completed project(except for some future changes).
How can i do this while keeping the new project in the same workspace as the original without:
creating a new project and copy pasting code over
copy-pasting the folder that holds the project and changing the name of the folder
I'm asking because there must be a better way to do this than the two options above.
Right click on the solution and "Open Folder in File Explorer"
Copy the project folder
Rename the new Project folder
Open the new project folder and rename the .csproj file
Right click on the solution and "Add" "Existing Project" to the solution
Open the "Properties" for the new project
Under "Application", change the assembly name and default namespace
Select "Tools", "Create GUID", Option 6, "Copy"
Under Assembly information, change the name and product
Paste in the GUID and clear the "" suffix
Rename the namespace in the source files to reflect the new
namespace from above.
Depending on your needs, you could get by with a subset of these steps (e.g. steps 1 and 2), however, I think a complete copy requires all of the steps mentioned.
The second option is exactly what you should do: duplicate the folder containing your project and then rename it.
To make this work, you might also have to change the GUID used by the project to avoid clashes. Visual Studio is supposed to do this automatically if another project with the same GUID already exists in the solution. If something goes wrong, you can do it manually by editing the project file (it's the field called ProjectGUID). It's just a standard GUID value, so use any freely-available GUID generator like the one bundled with Visual Studio or one you find online.
Why should there be a better option than this? What would that "better" option look like? It's not like there's going to be a menu command for it, this isn't a very commonly used feature. Most programmers use a source control system that handles branching/duplicating for them automatically and [mostly] without pain.

VS 2010 Unable to remove inherited additional dependencies

Hi by mistake I added cv210d.lib, cxcore210d.lib and highgui210d.lib as inherited values under the project configuration->Linker->input->Additional Dependencies settings.
But I cannot seem to remove them from the "inherited" additional dependencies. So each time I start a new project I get an error because these liberties cannot be found.
There is an option to ignore the inherited dependencies but I cannot do this as it contains values I do need.
So I am looking for a way to remove these unwanted values from the "inherited" additional dependencies.
You can find the file "Microsoft.Cpp.Win32.user" on your system and manually edit it as you want. Normally the file will be located at "C:\Users\%USERPROFILE%\AppData\Local\Microsoft\MSBuild\v4.0" location. It is an xml file.
As was suggested I modified the property sheet in the property manager.
The reason I found it confusing though was that in the Property Manager view, there are three different ways to change the properties. Through clicking on the debug, release property sheets or the project properties . But when I opened each one I thought I was opening the same thing, as they look similar.
But the case is that, the properties from the debug and release property sheets are inherited to the project properties. And you cannot modify the inherited properties from the project properties.
Did you accidentally add it to "Linker > Input" under "Property Manager > AnyProject > AnyConfiguration > Microsoft.Cpp.Win32.user"?
Despite its strange position, this is the setting for all projects of the current user.
As soon as you put a wrong .lib file into Additional Dependencies it went to the inherited directory. You need to untick "Inherit from parent or project defaults". Then, to save inherited .lib files (such kernel.lib and etc.) you need to copy and paste all of them into the Additional Dependencies directory. So your computer cannot take the wrong .lib file and will use only Additional Dependencies which you wrote
You can open with notepad the .vcxproj file of your project and find the lib you want to remove. Then, Delete that library from the xml field.

Visual Studio: can I copy a project's properties to use in another project?

I've added several directories, libraries such as matlab, opencv, etc to compile my current C file in Visual Studio project.
All my upcoming projects will require the same setup/properties. Do I need to setup my each project's properties individually or I can do something to apply it to all ?
Try using Property Sheets. These can create a single properties file that can be inherited by multiple projects.
Use "View > Other Windows > Property Manager" to bring up the Property Manager. It will show your projects and configurations.
Right click a configuration, and select "Add New Project Property Sheet...". You can name it, and select a location that works for all your projects.
Once created, you can edit its properties just as you would a build configuration.
To use that property sheet as the basis for other projects, just right click the configuration or project, and select "Add Existing Property Sheet...". Browse to where you saved the original sheet, and it will be inherited.
Any changes to the original sheet will be applied to any project that inherits it, unless they are overridden. If you go into properties higher up the chain, properties that override will have bold type for their values. To get them to always use the value in the base sheet, there will be a "inherit from parent or project defaults" option where applicable.
I am using AtmelStudio 6.1, which is built on Visual Studio 2010 (I believe), and yet doesn't have any Property Manager that I can find.
So, the hackish system I use is:
close (or at least unload via the contextual menu) the project whose configuration you want to modify
open its .cproj file (which is XML) in any text editor (VS works nicely),
MAKE A BACKUP COPY OF IT SOMEWHERE ELSE JUST IN CASE,
open the .cproj file of the project whose configuration data you want to copy,
look for the configuration data you want. Configurations are inside of PropertyGroup tags; for example, in my case it looks like
<PropertyGroup Condition=" '$(Configuration)' == 'Preprocess only' ">
for the configuration named "Preprocess only". Copy from the beginning of that tag until the end of the corresponding
</PropertyGroup>
tag.
Paste the block into the destination .cproj, just after other
</PropertyGroup>
tag. Make sure that the name of the configuration is unique in this file. Save.
You're finished. Now open the project normally in VS and you'll be able to select the added configuration.
For Visual Studio 2010+ you can make a Project Template.
If you choose to automatically add the template to Visual Studio in the template creation wizard, when you create/add a new project, the template will appear (after restarting Visual Studio). Projects created with this template will have the same project properties!
To add source code files to the template, the easiest way is to rearrange the source code files in solution-explorer to be in the root (not under any folder). After doing that, THEN generate the template.
Why? Sometimes putting your files under the default Source Code solution-explorer folder (not in file explorer, the .project.filters label) will fail to copy the source file to the template, you'll see "the document cannot be opened. it has been renamed deleted or moved" when you try to use the template and the file you wanted in the template will NOT be in the file explorer.
If you do want solution-explorer folders, you will have to add them manually to the template by unzipping the template folder, making changes, then re-zipping it again.
For example, literally copy and paste the source files you want the template to have in the template folder and edit the .vstemplate file. M$ Doc on template editing.
<TemplateContent>
<!-- put new file references here and/or in your .vcxproj .vcxproj.filters -->
TargetFileName="HelloWorld.cl">HelloWorld.cl</ProjectItem>
<ProjectItem ReplaceParameters="false"
</TemplateContent>
Related questions:
How do I use VS template I created?
How can i load a template I have created in Visual Studio?
How copy visual studio project?
Simply copying and pasting entire projects and solutions is not recommended for complex or shared projects since various GUID s and filenames may overlap - causing bugs.
For MSVS 2017, the process #AaronMK mentioned doesn't work. Instead do the following:
View -> Other Windows -> Property Manager
Add New Property Sheet.
Edit whatever options you want there.
Give it an appropriate name so that you remember it.
Right button on it and hit "Save {myPropertySheet}"
It would be wise to place it alongside the default property sheets and that you can add it whenever you want (by Property Manager -> Add Existing Property Sheet -> Browse to its location). This directory is: C:\Users\{myUsername}\AppData\Local\Microsoft\MSBuild\v4.0
Alternatively you can edit the default property sheets and use these by default. I wouldn't advice it though as they provide a fallback option in case you mess up. So make backups first if editing default files.

Change namespace/filesystem folder names in Visual Studio

I'm trying to change a namespace in Visual Studio.
My folder structure looks something like this:
GameAlpha/
GameAlpha.sln
GameAlphaRelease/
GameAlphaTest/
GameAlphaLevelEditor/
These include namespaces like GameAlphaRelease. I want to change all this to GameBetaRelease.
Before this process, it built fine.
First, I changed the solution and project files from Alpha to Beta. Then, I did a "find-replace-all" on the namespace. Finally, I went through the properties of each project and changed the "Assembly Name" and "Default Namespace" to the appropriate Beta title.
However, now the solution does not build. The error is:
GameAlpha.accessor: The reference to 'GameAlpha.exe' was not found in
the list of this projects references.
(Project: GameBetaTest)
What am I doing wrong? If I remove project GameBetaTest, the solution builds just fine.
Also, what is the preferable way to change the names of the folders in the file system?
The following steps normally work for me:
Use the standard project rename (this renames the project, but not the Project Directory). If you want to change the directory as well, close down the solution, rename the directory, open the solution, remove the old project (which is now unavailable) and add the project from the new location.
For each project for which it applies, remove and re-add references to other projects in the solution if there are any inter-project dependencies.
Adjust the project properties for each changed project.
Verify/adjust build scripts.
Verify/adjust the build order.
Clean and rebuild all.
If you do a package/class rename, make sure you do it separately (before, while everything is "still working") so that VS will update the internals as required. YMMV and there are some issues with files "linked" between projects.
Rename the physical project directory
Note: The physical path property is recorded in the .sln file so you cannot just rename the folder in Explorer.
a. Close the solution and the IDE
b. In Explorer: Change the directory name to the new name.
c. In Explorer: Open the .sln file with a text editor.
c. Change the directory name to the new name and save.
d. Restart the IDE and open the solution from the File, Recent Files menu if it doesn't start automatically.
e. Click on the Project folder of the Solution Explorer and check the path property in the properties at the bottom. It will now be referencing to the new project folder.
Here I found it

Possible to create Visual Studio project with Output Type of "none"?

I'm using Visual Studio 2008 and would like to create a sort of container project that holds a number of DLL's that must be installed with a solution. I want them to be in a separate project so that they can be easily attached to a solution as a group.
I created an empty project call TEST, added my DLL's to it with a Build Action of "Content", and set them to "Copy Always". That all works exactly as I want. The problem is that if I set the TEST project Output Type to "Console Application" or "Windows Application" that it won't build because there's no entry point. If I set the Output Type to "Class Library", it builds but I end up with an extra TEST.DLL file that I don't really want.
Is there anyway to sort of set the Output Type to "none"? I want the build actions to take place (so my DLL's get copied) but I don't want the dummy class assembly created. Any ideas?
Thanks!
Assumptions for the following step-by-step guide:
Let's assume that you have a solution with two projects:
Main: your main (start-up) project.
BundledDLLs: a library project which contains the .dlls that should end up in the main project's output directory.
Step-by-step guide:
The easiest way to achieve your goal inside Visual Studio is probably the following:
Add all .dlls to BundledDLLs and set their Copy to output directory to Copy if newer.
This is done in the Project Explorer and the Properties windows.
Configure BundledDLLs's output directory to be identical to Main's output directory.
This can be done in the Build tab of BundledDLL's Project Properties page. Enter something like the following in the Output Path textbox:
..\Main\bin\Debug
Set up BundledDLLs as a dependency of Main.
Do not add BundledDLLs as a project reference to Main, as you usually might; instead, use the Project Dependencies dialog to . This will tell the build tool that whenever Main is built, BundledDLLs needs to be built first.
Do this by right-clicking on the Main project node to open the context menu; select Project dependencies... from there. In the now opened dialog, first select Main from the drop-down list; then check BundledDLLs in the project list below. BundledDLLs is now registered as a dependency of Main.
P.S.: One disadvantage of not having an explicit assembly reference in Main is that some tooling might not recognise the dependency. For example, ClickOnce deployment might not work properly.
Add a post-build event to BundledDLLs that deletes the superfluous BundledDLLs.dll.
As you said, you don't want, and don't need, the dummy output generated when BundledDLLs is built. So add a post-build event that simply deletes this .dll once it's been created.
Open the Build events tab in BundledDLLs's Project Properties page, and enter something like the following in the post-build textbox:
DEL "$(TargetDir)\$(TargetName).*"
(In case you wondered: The reason why you didn't add this project as a project reference to Main earlier is because if you had done so, Main would be looking for BundledDLLs.dll, which it wouldn't be able to find since you don't actually want such a file to be generated.)
P.S.: One disadvantage of adding such a post-build step is that it might interfere with incremental builds. If your project keeps getting recompiled from scratch after this, you might be better off removing the post-build step and living with the extra BundledDLLs.dll in your solution's output directory.
Another option is to use a makefile project, which doesn't require you to build/link anything.
In your project properties (right click property in solution explorer and click "Properties"), under "Configuration Properties" and then under "General", choose "Makefile" from the "Configuration Type" drop-down menu. The build output will include the warning "The property 'NMakeBuildCommandLine' doesn't exist...Skipping" but the build will succeed without building any dll/exe/etc.
While other answers here may better address your specific need, specifying a makefile more directly answers the question title "Possible to create Visual Studio project with Output Type of none?" I hope this is useful for people who google something to that effect and land here.
Credit goes to Xeek in the #winapi freenode irc channel for sharing this tip.
Instead of putting them in a project, you can put the files in a Solution Folder. One of your projects can have a build action that does the copying, but since they won't be in a project, they won't try to "build".

Resources