Review Solution Actions: Retarget Projects - how to suppress it? - visual-studio

The following dialog prompts me to upgrade the platform toolset and Windows SDK version of files generated for VS2017 when opening it in VS2019:
How can I suppress this and have the action being taken to be No Upgrade?
After all I can still opt to do that later on via the context menu when right clicking a project in the Solution Explorer:
devenv.com from Visual Studio 2019 has:
/Upgrade Upgrades the project or the solution and all projects in it.
A backup of these files will be created as appropriate. Please
see Help on 'Visual Studio Conversion Wizard' for more
information on the backup process.
... what I'd like is essentially the opposite, though. And I'd also like for that setting to persist.
Note: while the screenshots were taken with Visual Studio 2019, earlier Visual Studio versions (notably since 2012) show this behavior as these appear to be the ones to support multiple platform toolsets and Windows SDKs in parallel. Therefore I am also tagging visual-studio.
Searching the options I came up emptyhanded when looking for:
action
retarget
target
upgrade
sdk
platform
toolset

You can suppress this dialog per-project by editing the project file, ProjectName.vcxproj.
It is an XML file. You can edit it with any text editor. Inside the <Project> tag, add the following lines:
<PropertyGroup Label="Globals">
<VCProjectUpgraderObjectName>NoUpgrade</VCProjectUpgraderObjectName>
</PropertyGroup>
Source: https://learn.microsoft.com/visualstudio/extensibility/visual-cpp-project-extensibility#disable-project-upgrade

Related

VS 2019 could not find SDK

I keep getting this error, I am trying to perform the unity MRTK tutorial for hololens 2. I did everything on the first page https://learn.microsoft.com/en-us/windows/mixed-reality/mr-learning-base-02. When I try and run it this error comes up. I have tried to install 10.0.19041.0 through windows but it always kicks to 19041.1 I have installed all the required components in visual studio.
Any help would be appreciated.
Error pic
Installed Vs individual components
Unity Build settings
The problem you met is a bug seemingly caused by vs installer. It happens when Visual Studio is not installed on driver C.
You can see it here:
https://developercommunity2.visualstudio.com/t/HoloLens-2-build-fails-if-Windows-sdk-is/1313568
There are many ways you could do to get through this.
Copy the folder [Windows Kit Root Dir]\10\Extension SDKs\WindowsMobile (For e.g., D:\Windows Kits\10\Extension SDKs\WindowsMobile) to C:\Program Files (x86)\Windows Kits\10\Extension SDKs\. Should solve all of the missing WindowMobile issues.
https://developercommunity.visualstudio.com/t/msb3774-could-not-find-sdk-windowsmobile-version10/402362#T-N474809
Delete the reference to the Mobile SDK in the vxcproj file.
https://stackoverflow.com/a/60600054/9522354
I tried the first method and it worked for me.
I have tried to install 10.0.19041.0 through windows but it always kicks to 19041.1
For how to modify the SDKReference version, you can try following step:
By referring Section 5 of Update your C#/VB projects to use the latest Universal Windows Platform
Right-click your existing project(YourProjectName(Universal Windows)) in solution explorer, then select Unload Project. After the project is unloaded, right-click the project file again and choose to edit the .vcxproj file.
Find "SDKReference" element and change the value of version from "10.0.19041.1" to your version like "10.0.19041.0".
<ItemGroup>
<SDKReference Include="WindowsMobile, Version=10.0.19041.1" />
</ItemGroup>
to
<ItemGroup>
<SDKReference Include="WindowsMobile, Version=10.0.19041.0" />
</ItemGroup>
Besides, since you are using HoloLens2, it is recommended to set the Architecture in the Unity Build Setting window to x64.

TypeScript VsTsc error in Visual Studio 2017

I'm testing migration from VS 2015 to VS 2017 for a .NET Core / TypeScript project.
The build fails in VS 2017 with this error in Microsoft.TypeScript.targets:
MSB4064 The "PreferredUILang" parameter is not supported by the "VsTsc" task. Verify the parameter exists on the task, and it is a settable public instance property.
The version of TypeScript in VS 2017 is 2.1.5, however, I have already installed the 2.2.1 SDK for Visual Studio 2015.
I've also noted that there is no 2.2+ SDK release for Visual Studio 2017 yet.
Could this be causing conflicts? Is this something I can resolve now or do I need to wait for an update to the TypeScript SDK for VS 2017 to reach 2.2.1+?
Any help in this area appreciated!
This might be a bit of crude solution to the problem but we simply went through the "Microsoft.TypeScript.targets" file and removed PreferredUILang="$(PreferredUILang)" from any <VsTsc ... > nodes, we had a look into the Typescript task dll and it seems that it does not have a PreferredUILang property.
It is possible that the task once upon a time did have such a property but was removed and Microsoft have forgotten to update the targets file, I'm not sure but this seems to be working for us at least.
Please make sure you backup your "Microsoft.TypeScript.targets" file before editing.
I have same problem here in VS2015 Update 3 and I also fixed the Microsoft.TypeScript.targets, as suggested by ginja, but as I don't like to hack the nuget packages I went deeper in the issue.
The real problem is that when you add/upgrade the typescript nuget package, you have to manually remove the imports to the machine-wide targets and props. having both leads to unknown load order or targets/tasks, which would manifest in such errors.
So the approach I used is:
Uninstall from the project the nuget packages "Microsoft.TypeScript.Compiler" and Microsoft.Typescript.MSBuild"
Close VS (to guarantee the real clean of such package)
Edit you .csproj file commenting out the imports to the machine-wide targets and props: (all <Import Project="$(MSBuildExtensionsPath32)\Microsoft\...
Restart VS, open the project and add again the nuget packages for Typescript. This point correctly updates your .csproj with the correct imports and configuration of the TypeScript compiler.
Just for a clean safe: restart VS.
removing following lines from CSPROJ solved problem for me
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.Default.props" Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.Default.props')" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets" Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets')" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets" Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets')" />
to edit csproj file
Right click the project in Solution Explorer.
Select "Unload project"
Project is now unloaded.
Right click the project again and select
"Edit blah.csproj"
Make your changes and save them.

Cross-targeting frameworks with NuGet 4.0 and Visual Studio 2017

I am having a rough time figuring out how to setup cross-targeting inside a Visual Studio 2017 project and I have not been able to find any examples.
I started out with a .NET Standard 1.5 project and to keep it simple I am just trying to add .NET Standard 1.6. If I understand the documentation correctly, I should now be able to do all of this inside the csproj file without having to mess with a project.json or nuspec file.
I've tried all of these values but none seem to work:
<TargetFrameworks>netstandard15;netstandard16</TargetFrameworks>
<TargetFrameworks>netstandard1.5;netstandard1.6</TargetFrameworks>
<TargetFrameworks>.NETStandard,Version=v1.5;.NETStandard,Version=v1.6</TargetFrameworks>
This is the only source of documentation I can find on the feature and it doesn't contain a full example:
https://docs.nuget.org/ndocs/schema/msbuild-targets
https://docs.nuget.org/ndocs/create-packages/supporting-multiple-target-frameworks
I've gotten this to work on latest Visual Studio 2017. As described in this post https://blogs.msdn.microsoft.com/dotnet/2016/10/19/net-core-tooling-in-visual-studio-15/ it is the correct way to do it. My csproj file looks like this:
<PropertyGroup>
<TargetFrameworks>netstandard1.6;net452</TargetFrameworks>
</PropertyGroup>
Visual Studio 2017 RC release notes also has this listed as a feature (under .NET Core and Docker):
Cross-target multiple target frameworks in one project.
My mistake at the start was that when I first created the project the property was called TargetFramework, I tried to add multiple targets and VS did not like that at all. It just crashes then... So make sure to rename it to TargetFrameworks and it should work.

Setting executable icon in Visual C++ 2010 Express

I'm rather unfamiliar with programming tools in Windows but have been forced to use VC++ 2010 Express for a project recently. We have been working on a piece of software that we didn't start and now we are reaching the finish line. Only problem is that I want to be able to set the executable icon.
Since the project is written mostly in SDL we have set the titlebar icon using the SDL_WM_SetIcon call as recommende on several sites but I still can't find any references to how to set the executable icon. All my google searches has turned up results about Visual Studio 2010 and Visual C++ 2008, neither of which seems appicable.
Since VC++ 2010 Express lacks resource editor but not resource compiler this can be done by manually creating a resource.rc file in the project directory (same as where your .vcxproj files are located), also drop in your .ico version of your icon in the same directory.
In resource.rc add the following line:
IDI_APP ICON "icon.ico"
And add the file, in VC++, to the Resource Files.
Doing this in VC++ should result in something like this to be added to your .vcxproj:
<ItemGroup>
<ResourceCompile Include="resource.rc" />
</ItemGroup>
And to your vcxproj.filters:
<ItemGroup>
<ResourceCompile Include="resource.rc">
<Filter>Resource Files</Filter>
</ResourceCompile>
</ItemGroup>
I ran into a similar situation recently with the Express edition.
I came up with a different way to approach this (see gif).
Posting it here in case someone runs into similar problem.

Removing all unused references from a project in Visual Studio projects

I just wondered if it possible within various Visual Studio versions to automatically remove all references from a project that were never been used?
In your answer, please specify which version of VS the solution applies to.
If you have Resharper (plugin) installed, you can access a feature that allows you to analyze used references via Solution Explorer > (right click) References > Optimize References...
http://www.jetbrains.com/resharper/webhelp/Refactorings__Remove_Unused_References.html
This feature does not correctly handle:
Dependency injected assemblies
Dynamically loaded assemblies (Assembly.LoadFile)
Native code assemblies loaded through interop
ActiveX controls (COM interop)
Other creative ways of loading assemblies
All you need is stone and bare knuckle then you can do it like a caveman.
Remove unused namespaces (for each class)
Run Debug build
Copy your executable and remaining namespace references to new location
Run the executable
Missing Reference DLL error will occur
Copy required DLL from Debug folder
Repeat 4-6
Gu Gu Ga Ga?
Throw your stone
You can also rely on your build tools to let you know which reference is still required. It's the era of VS 2017, caveman still survived.
The Resharper extension will do this for you.
This extension supports Visual Studio 2005 through 2017.
While the compiler won't include unused assemblies, extraneous using statements and references slows down Visual Studio and Intellisense, since there's more code the tools have to consider.
You can try the free VS2010 extension: Reference Assistant by Lardite group. It works perfectly for me. This tool helps to find unused references and allows you to choose which references should be removed.
In a Visual Basic project there is support to remove "Unused References" (Project-->References-->Unused References). In C# there isn´t such a function.
The only way to do it in a C# project (without other tools) is to remove possible unused assemblies, compile the project and verify if any errors occur during compilation. If none errors occur you have removed a unused assembly. (See my post)
If you want to know which project (assembly) depends on other assemblies you can use NDepend.
With Visual Studio versions 2017 and 2015, you can do this with the Code Map feature, but this feature is only available in the Enterprise Edition, not the Community or Professional versions.
Right-click on the project node in the solution explorer and select 'Show on Code Map.' This will display your .dll as a single node in a blank graph. Right-click on that node in the Code Map and select "Show Assemblies This References." This will add an additional node called "Externals" which can be expanded to show only the assemblies that are actually referenced.
For Visual Studio 2013/2015/2017 there is an extension that does exactly what you want: ResolveUR. What this basically does is:
reference is removed in the project
project is compiled with msbuild
check for build errors
restore removed references if there were build errors.
For anybody coming here looking for Visual studio 2012:
Download and Install Reference Assistant for Visual Studio 11
Later you can do:
In Visual Studio 2013 this extension works:
ResolveUR
Some people suggested to use an awesome tool - Reference Assistant for Visual Studio. The problem is that VS2012 is the latest supported Visual Studio. But there is the way to make it work in VS2013 as well ;)
And here is how:
1) Download Lardite.RefAssistant.11.0.vsix
2) Change the extension to zip: Lardite.RefAssistant.11.0.vsix -> Lardite.RefAssistant.11.0.zip
3) Unzip and open the extension.vsixmanifest file in the text editor
4) Find all occurences of InstallationTarget Version="[11.0,12.0)" and replace them with InstallationTarget Version="[11.0,12.0]" (note the closing bracket)
5) Save the file and zip all files so they are on the root zip level
6) Change the extension of the new zip to vsix
7) Install and enjoy :)
I've tested it with VS2013, thanks source for the tutorial
EDIT
Add to support VS 2015 Community Edition
<InstallationTarget Version="[14.0,15.0]" Id="Microsoft.VisualStudio.Community" />
Meaning of the brackets
[ – minimum version inclusive.
] – maximum version inclusive.
( – minimum version exclusive.
) – maximum version exclusive.
[Update] This feature is only available for .Net core projects.
This feature will be coming to Visual Studio 2019 very soon and already available with Visual Studio 2019 v16.10 Preview 1.
This option is turned off by default, but you can enable it under menu Tools > Options > Text Editor > C# > Advanced. Select the Remove Unused References command in Solution Explorer (Experimental). Once the option is enabled, the Remove Unused References command will appear in the right-click menu of a project name or dependencies node.
You can use Reference Assistant extension from the Visual Studio extension gallery.
Used and works for Visual Studio 2010.
In the VS2022 (preview at the moment of writing) this comes out of the box for SDK Style Projects (read: .NET Core and newer).
If it is available you can find it in the project context menu:
You get to choose what to do with each finding.
Read more about it here.
Pro-tip: Check if your project compiles and runs correctly after applying this. In my experience it doesn't check whether a dependency is used at runtime, for instance.
Using DevExpress, I follow these instructions:
In VS, go to DevExpress - Editor - Code Cleanup. Under Rules, check 'Remove unused namespace references'. Click OK.
Right-click on the solution, and choose 'Code Cleanup'. The cleanup runs for a few minutes, and finishes.
Build your application
The following method does not depend on any 'add-on's and is not very painful.
Step through each of your source files and
Select all (Ctrl-A)
Toggle outline expansion (Ctrl-M, M). This will reduce the file to two lines.
Click on the namespace's '+'. This will show each of the file's classes as a single line. Scan each class's reference count, looking for unreferenced classes.
Click on each of the classes' '+'. This will show each of the class functions as a single line. Scan each function's reference count, looking for unreferenced functions.
Scanning each file looking for '0 reference' takes only a second.
Scanning an entire project takes only a couple of minutes.
In VB2008, it works this way:
Project>Add References
Then click on the Recent tab where you can see list of references used recently. Locate the one you do not want and delet it. Then you close without adding anything.

Resources