MSB3021 not copy file -> access denied - c#

I wnat to change some code in application. I decompose some libs and want to compile changed files. I have next csproj file:
<PropertyGroup>
<AssemblyName>Siemens.Simatic.Hwcn.Basics</AssemblyName>
<GenerateAssemblyInfo>False</GenerateAssemblyInfo>
<UseWindowsForms>True</UseWindowsForms>
<TargetFramework>net461</TargetFramework>
<PlatformTarget>x64</PlatformTarget>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
</PropertyGroup>
<PropertyGroup>
<LangVersion>7</LangVersion>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup>
<RootNamespace />
<OutputPath>C:\Program Files\Siemens\Automation\Portal V15_1\Bin</OutputPath>
</PropertyGroup>
And when I build solution display error:
Unable to copy file "obj\Debug\Siemens.Simatic.Hwcn.Basics.dll" в "C:\Program Files\Siemens\Automation\Portal V15_1\Bin\Siemens.Simatic.Hwcn.Basics.dll". The process cannot access the file "C:\Program Files\Siemens\Automation\Portal V15_1\Bin\Siemens.Simatic.Hwcn.Basics.dll".

Just open Visual Studio with admin access

Related

visual studio customize the default build properties

MyApp.csproj
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<UseWindowsForms>true</UseWindowsForms>
<TargetFrameworks>net48;net5.0-windows</TargetFrameworks>
<Configurations>Release</Configurations>
<Platforms>AnyCPU;x64;x86</Platforms>
<RuntimeIdentifiers>win-x64;win-x86</RuntimeIdentifiers>
<SelfContained>false</SelfContained>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64' And '$(TargetFramework)'=='net5.0-windows' And '$(RuntimeIdentifier)'=='win-x64'">
<OutputPath>..\..\app\</OutputPath>
<AssemblyName>my-app</AssemblyName>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86' And '$(TargetFramework)'=='net5.0-windows' And '$(RuntimeIdentifier)'=='win-x86'">
<OutputPath>..\..\x86\app\</OutputPath>
<AssemblyName>my-app</AssemblyName>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
</PropertyGroup>
</Project>
these commands build both x64 and x86 assemblies in "....\app" and "....\x86\app" folders
msbuild.exe "MyApp.csproj" /t:restore;rebuild /p:RuntimeIdentifier=win-x64
msbuild.exe "MyApp.csproj" /t:restore;rebuild /p:RuntimeIdentifier=win-x86
but clicking Build (Ctrl+F5) or Debug (F5) in Visual Studio outputs to "bin\Release\net5.0-windows" directory.
how to force Build (Ctrl+F5) and Debug (F5) to do the same as this command:
msbuild.exe "MyApp.csproj" /t:restore;rebuild /p:RuntimeIdentifier=win-x64
Here is my code:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFrameworks>net5.0-windows;net472</TargetFrameworks>
<Nullable>disable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<Configurations>Release</Configurations>
<Platforms>AnyCPU;x64;x86</Platforms>
<SelfContained>false</SelfContained>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<OutputPath>..\..\app\</OutputPath>
<AssemblyName>my-app</AssemblyName>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'">
<OutputPath>..\..\x86\app\</OutputPath>
<AssemblyName>my-app</AssemblyName>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
</PropertyGroup>
</Project>
You can refer to the following steps to solve this problem:
1.Select target framework .net5.0-windows
2.Choose Platform x86
3.Debug your project with the button or F5
4.Change Platform x64 and Debug it.
5.Finally we can get these folders.

Completely change obj folder location in c# project

MSBuild is strange
I already tried this and another answer and I also tried this one
After that, I changed <IntermediateOutputPath> and <BaseIntermediateOutputPath> and <OutputPath> in the .csproj file but...
It keeps creating this piece of strange stuff in the old obj folder (I don't use nuget)
project.assets.json
project.nuget.cache
project.packagespec.json
...
I have already read about Visual Studio legacy workflow causes this behaviour but do any workarounds exist?
My current .csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<StartupObject>Program</StartupObject>
<IntermediateOutputPath>..\..\obj\</IntermediateOutputPath>
<BaseIntermediateOutputPath>..\..\obj\</BaseIntermediateOutputPath>
<OutputPath>..\..\bin\Build\</OutputPath>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="foo\dependency.csproj" />
</ItemGroup>
</Project>
Solved by creating Directory.Build.props file in the root of project with:
<Project>
<PropertyGroup>
<MSBUildProjectExtensionsPath>..\..\obj\</MSBUildProjectExtensionsPath>
</PropertyGroup>
</Project>
Very dirty and non-obvious microsoft-style hack
Found here
Is there any good .NET compiler for windows without penetrating youself?

How prevent copy unnecessary libraries on publish

I have three simple projects:
SampleApp.Cli1
SampleApp.Cli2
SampleApp.Lib
SampleApp.Cli1 use Microsoft.Extensions.Configuration directly
SampleApp.Cli2 uses Microsoft.Extensions.Configuration through SampleApp.Lib
SampleApp.Cli1:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<OutputType>Exe</OutputType>
<OutputPath>../_out1</OutputPath>
<TargetFramework>netcoreapp3.1</TargetFramework>
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
</PropertyGroup>
</Project>
SampleApp.Cli2:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<OutputType>Exe</OutputType>
<OutputPath>../_out2</OutputPath>
<TargetFramework>netcoreapp3.1</TargetFramework>
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\SampleApp.Lib\SampleApp.Lib.csproj" />
</ItemGroup>
</Project>
SampleApp.Lib:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.6" />
</ItemGroup>
</Project>
Script for publishing:
#echo off
if exist "%~dp0_out1" (
rd "%~dp0_out1" /s /q
)
if exist "%~dp0_out2" (
rd "%~dp0_out2" /s /q
)
dotnet publish "%~dp0SampleApp.Cli1/SampleApp.Cli1.csproj"
dotnet publish "%~dp0SampleApp.Cli2/SampleApp.Cli2.csproj"
After build
publish dir for SampleApp.Cli1 contains this files
web.config
SampleApp.Cli1.deps.json
SampleApp.Cli1.dll
SampleApp.Cli1.exe
SampleApp.Cli1.pdb
SampleApp.Cli1.runtimeconfig.json
publish dir for SampleApp.Cli2 contains this files
SampleApp.Lib.pdb
web.config
Microsoft.Extensions.Configuration.Abstractions.dll
Microsoft.Extensions.Configuration.dll
Microsoft.Extensions.Primitives.dll
SampleApp.Cli2.deps.json
SampleApp.Cli2.dll
SampleApp.Cli2.exe
SampleApp.Cli2.pdb
SampleApp.Cli2.runtimeconfig.json
SampleApp.Lib.dll
Question
For some unknown for me reason, the publication understands that for SampleApp.Cli1 Microsoft.Extensions.* must be taken from the shared runtime(Program Files\dotnet\shared\Microsoft.AspNetCore.App), but does not understand the same for SampleApp.Cli2. Of course, I understand that the matter is most likely in SampleApp.Lib(in real application it was Microsoft.Extensions.Hosting.WindowsServices).
In my situation, I have to build all libraries from sources that go into the publish folder. In real application Microsoft.Extensions.* libraries much more, and I would not really like to build all libraries from asp net core runtime.
Is any way to prevent copy(publish) Microsoft.Extensions.* libraries?
Sdk: 3.1.6

Create a self contained exe with a COMReference

I am trying to publish a self contained exe file.
Sadly all my google work was not helpful. I have a COM reference in my code to the WindowsInstaller.
So far this is my csproj file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>MSI_Info</RootNamespace>
<RuntimeIdentifiers>win10-x64;win10-x86</RuntimeIdentifiers>
</PropertyGroup>
<ItemGroup>
<COMReference Include="WindowsInstaller">
<Guid>{000C1092-0000-0000-C000-000000000046}</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>1033</Lcid>
<WrapperTool>tlbimp</WrapperTool>
<Isolated>False</Isolated>
<EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference>
</ItemGroup>
</Project>
Code is hosted here https://github.com/SmuSmu/MSI-Info
Not so easy to get a simple exe file :)

Multiple binaries from a single DotNet Core project configuration file

I have code for a bunch of CLI utilities made to test/showcase a network library. The library is compiled into an assembly - DotNet Core DLL.
I have several CLI examples showing how to use the library, for example, one search is using paging functionality and another returns everything etc. Basically, each is a short standalone CLI program.
I have CS source files and csproj file targeting dotnet core. Below is the configuration:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Reference Include="mylib>
<HintPath>../../bin/Debug/netstandard2.0/publish/mylib.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
I want to have one executable for each source file e.g. PGSample.cs will get compiled into PGSample.exe etc. How would I about achieving this?
I'm afraid you can have only one output per csproj, but there are some tricks to manage multiple projects easier.
You can put common build settings into a file named Directory.build.props in the root project folder, e.g.:
<Project>
<PropertyGroup>
<Authors>Your name here</Authors>
<Company>Your company</Company>
<Product>The project name</Product>
<Version>1.6.18</Version>
<PackageLicenseExpression>GPL-3.0-or-later</PackageLicenseExpression>
<!-- e.g. bin/Release_net48/foo_cli/ -->
<OutputPath>$(SolutionDir)bin\$(Configuration)_$(TargetFramework)\$(MSBuildProjectName)</OutputPath>
<TargetFrameworks>net48</TargetFrameworks>
</PropertyGroup>
</Project>
Then, add one subdirectory and minimal csproj file per output, e.g.
libfoo/libfoo.csproj: <Project Sdk="Microsoft.NET.Sdk" /> (really, that's it!)
foo_cli/foo_cli.csproj:
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup><OutputType>Exe</OutputType></PropertyGroup>
<ItemGroup><ProjectReference Include="..\libfoo\libfoo.csproj" /></ItemGroup>
</Project>
Iff you have only one library and a lot of executables, you might even add your executable settings to the Directory.build.props:
[..]
<PropertyGroup Condition=" $(MSBuildProjectName) != 'libfoo'">
<OutputType>exe</OutputType>
</PropertyGroup>
<ItemGroup Condition=" $(MSBuildProjectName) != 'libfoo'">
<ProjectReference Include="..\libfoo\libfoo.csproj" />
</ItemGroup>

Categories