在 windows 中编写一个可执行的应用程序

问题描述

我想写一个工具来部署我的应用程序,基本上这个东西需要两部分:

  1. 删除文件夹中除 web.config 文件外的所有内容
  2. 文件夹中的所有内容转移到另一个文件夹中

在这个问题我只想完成第一个问题,我们一直在做手工部署,所以我想提高我的效率。

这是我的想法。我基本上有两个想法:

  1. 在 Visual Studio 中编写一个应用程序,也许是在 C# 中,然后输入文件名称,在代码中设置一些东西,删除web.config 之外的所有内容
  2. 编写一个简单的脚本,我可以在 cmd 中运行它,然后执行它。

我更喜欢第二个,因为脚本不会那么难调整,而且也会很简单。

我的问题是:如果我想写这个脚本,我应该如何启动它?就像你点击那种文件一样,你会看到一个黑色的 DOS 窗口,你输入运行,然后这些文件夹中的所有内容都将消失。有人能给我一个起点吗?

非常感谢!

解决方法

您可能可以制作一个简单的控制台应用程序来从您的 bin 文件夹(发布或调试)复制所有内容并覆盖应用程序工件(减去配置文件)。这基本上就是 Team City、AzureDevOps 和许多其他 DevOps 工具所做的。但是,在后台,他们使用 powershell。如果您的应用程序在替换您的应用程序工件之前没有任何需要停止的服务,您可以使用如下所示的内容。如果您需要停止和启动服务,那也很简单,假设您有权限这样做。这可以编译为可执行文件,或保存为 .ps1 文件。可以从 Powershell(Invoke-Item 命令或右键单击并使用 Powershell 运行)或在命令窗口 (https://www.howtogeek.com/674537/how-to-find-and-open-files-using-command-prompt/)

中运行
# If you need to stop services
Get-Service -Name $serviceName -ComputerName $computerName | Set-Service -Status Stopped;

# Variables
$folder = "C:\CODE\ApplicationFolder\Web\bin\Release"
$webConfig = "WebConfig.exe.config"
$destinationPath = "\\serverName\folderPath\"
$destinationFolder = "Release"

# Zip existing folder and rename for backup
Compress-Archive -Path $destinationPath -DestinationPath $destinationPath\ApplicationArtifacts_Backup_$(Get-Date -Format MMddyyyy).zip | Out-Default

# Get Updated application deployment artifacts
Copy-Item -Path $folder\* -Exclude $webConfig -Destination $destinationPath$destinationFolder -Recurse -Force | Out-Default

# If you need to start services
Get-Service -Name $serviceName -ComputerName $computerName | Set-Service -Status Running;