Change ClickOnce settings depending on the build configuration you have selected (.NET Framework version only)

Page update date :
Page creation date :

environment

framework
  • Windows Forms (.NET Framework) in general
  • WPF (.NET Framework) in general

premise

The contents of these tips are project assumptions for the .NET Framework. In the case of .NET (Core), you can set ClickOnce publishing settings in the build configuration unit, so please publish as usual.

It also assumes that you have enough knowledge to use ClickOnce normally.

At first

Because a .NET Framework project can have only one ClickOnce publishing setting, For example, to publish with different settings in a Debug build and a Release build, you must manually change the settings each time before publishing.

You can have two settings by grouping the projects into a DLL and creating two separate projects for publishing. You will have the same configuration twice, and if there are resource files necessary for publishing, you must have two of them at a time, so management will be troublesome.

This section describes how to create a batch file (.bat) as many settings as you need and publish ClickOnce.

Configuring ClickOnce

This time, "Public Folder Location" and "Installation Folder URL" were separated for "Debug Build" and "Release Build". Other settings are created with the policy of making them common. Other parameters can be separate, but it can be cumbersome to keep the "application files" and "prerequisites" separate.

First, open the project and enter your ClickOnce settings as usual. The input contents are set to parameters to be issued by Debug, but anything other than the common part is fine as long as it does not cause an error.

The following is an example of the configuration:

Once you've typed, save the project.

Create a .bat file for ClickOnce publishing

Since it is issued with a command, it can be executed on other media such as PowerShell, but this time it is created as a BAT file.

You can create the file anywhere, but for simplicity purposes, I'll create it in the location of the solution file (.sln). Since it is a BAT file, the character encoding is Shift-JIS. PS1 can be UTF-8 (BOM).

Open the BAT file in a text editor and type: Note that PowerShell may handle escaping differently.

call "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat"
cd %~dp0
msbuild ClickOnceBuildConfiguration.sln /t:Publish /p:Configuration=Debug /p:PublishUrl="publish\Debug\\" /p:InstallUrl="\\xxxxServer\publish_Debug\\"

The first line points to the location of "Developer Command Prompt". This path depends on the version of Visual Studio you are installing.

Each part of the path depends on the following conditions:

Program Files
Path Conditions
Program Files
  • If you are using a 32-bit OS
  • If you are using a 64-bit version of Visual Studio on a 64-bit OS
Program Files (x86)
  • If you are using a 32-bit version of Visual Studio on a 64-bit OS
2022
It depends on the version of Visual Studio you are installing.
Community
It depends on the edition of Visual Studio you are installing. "Community", "Professional", "Enterprise", etc. will be entered.

The second line moves the current directory to the location of the BAT file.

The third line is the ClickOnce build processing command. The meaning of each parameter is as follows.

parameter name description
msbuild A command that executes the build process. Required and written first.
ClickOnceBuildConfiguration.sln Describes the file name of the solution to be built.
/t:Publish Indicates a ClickOnce build process.
/p:Configuration=Debug Specifies the build configuration that you have set for your solution. If you want to change the debug configuration to be built, change the text in the "Debug" part.
/p:PublishUrl="publish\Debug\\" Overwrite the value of "Public folder location" on the Visual Studio publishing screen with the specified string and execute it.
/p:InstallUrl="\\xxxxServer\publish_Debug\\" Overwrite the value of "Installation folder URL" on the Visual Studio publishing screen with the specified string and execute it.

Some of the parameters have been extracted, but see the following links for other parameters:

As a sample this time, we will change the publishing parameters of ClickOnce in each debug and Release build, so we will create it as follows.

Published by ClickOnceDebug.bat

call "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat"
cd %~dp0
msbuild ClickOnceBuildConfiguration.sln /t:Publish /p:Configuration=Debug /p:PublishUrl="publish\Debug\\" /p:InstallUrl="\\xxxxServer\publish_Debug\\"

ClickOnceRelease Published .bat

call "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat"
cd %~dp0
msbuild ClickOnceBuildConfiguration.sln /t:Publish /p:Configuration=Release /p:PublishUrl="publish\Release\\" /p:InstallUrl="\\xxxxServer\publish_Release\\"

ClickOnce Publishes BAT File Execution

Try running each BAT file.

The command prompt is launched and the build starts.

Actually, the BAT file created this time is only a process until the build, so the file is not created in the "public folder location". The files necessary for publication have been created. For example, in the case of a debug build, you should have a file in the "< project folder path >\bin\Debug\app.publish\". Copy these files to the "URL of the installation folder" and it will work correctly as ClickOnce.

Publish to a public folder location (if the destination is a folder that you have direct access to)

The following site is used as a reference for this part.

This area is not directly related to the processing of ClickOnce, so please refer to the above site for a detailed explanation.

First, open the project file (.csproj) in a text editor. It is not a solution file.

Inside is an XML structure, and if you scroll to the bottom, add the </Project> following code to the line above it. If it is a file copy premise process, the contents are fixed, but the part of is CopyPublishedApplication arbitrary. You'll use this name later.

<!-- ここから ClickOnce の発行処理 -->
<Target Name="CopyPublishedApplication">
  <ItemGroup>
    <MySourceFiles Include="$(PublishDir)**\*.*" Exclude="$(PublishDir)$(AssemblyName).exe" />
  </ItemGroup>
  <PropertyGroup>
    <AppricationDir>$(_DeploymentApplicationDir.Substring($(PublishDir.Length)))</AppricationDir>
  </PropertyGroup>
  <Copy SourceFiles="@(MySourceFiles)" DestinationFiles="@(MySourceFiles->'$(PublishUrl)%(RecursiveDir)%(Filename)%(Extension)')" />
</Target>
<!-- ここまで ClickOnce の発行処理 -->

Save it once you have entered it.

Then open each BAT file and add to the /t:CopyPublishedApplication msbuild parameters. CopyPublishedApplication is the name I gave you earlier.

ClickOnceDebug publishes.bat (example)

call "C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat"
cd %~dp0
msbuild ClickOnceBuildConfiguration.sln /t:Publish /t:CopyPublishedApplication /p:Configuration=Debug /p:PublishUrl="publish\Debug\\" /p:InstallUrl="\\xxxxServer\publish_Debug\\"

When you run the BAT file, you can see that the file is copied to the Public Folder Location. At this time, there is no < program name >.exe file, but it is unnecessary because it is originally included in setup.exe.

supplement

If there are multiple projects in the solution, runs CopyPublishedApplication for all projects. In most cases, it is only set to the exe project, so an error will be displayed when running in CopyPublishedApplication another project, It is safe to ignore it because it only displays a message.

Publish to a public folder location (for FTP)

I haven't tried it, but you may want to refer to the following sites.

Other things to keep in mind