我希望通过 c# 代码使用 MSDeploy API 远程触发删除命令。
我想实现以下命令:
msdeploy.exe -verb:delete -dest:contentPath="/folderName/filename.txt"
而不是通过运行非托管的外部可执行文件,我想使用 MSDeploy .Net API 来执行它。
假设您正在尝试删除绝对文件路径(而不是网站中的文件),您正在寻找这样的东西:
DeploymentObject destObject = DeploymentManager.CreateObject(
DeploymentWellKnownProvider.FilePath, "/foldername/filename.txt");
DeploymentObject sourceObject = DeploymentManager.CreateObject("auto", "");
DeploymentBaseOptions baseOptions = new DeploymentBaseOptions();
DeploymentSyncOptions syncOptions = new DeploymentSyncOptions
{
DeleteDestination = true;
};
DeploymentChangeSummary results = sourceObject.SyncTo(
destObject, baseOptions, syncOptions);
// results.ObjectsDeleted == 1
感谢 Richard Szalay 的领导,我找到了答案,并且我使用了ContentPath
提供程序,因为这是 VS Publishing 使用的常见提供程序,因此获得权限的机会很高:
var deployBaseOptions = new DeploymentBaseOptions
{
ComputerName = "https://mywebserver.com:8172/msdeploy.axd?sitename=yourIISWebsiteName",
UserName = "username",
Password = "password",
UseDelegation = true,
AuthenticationType = "Basic"
};
var syncOptions = new DeploymentSyncOptions
{
DeleteDestination = true
};
var deploymentObject = DeploymentManager.CreateObject(DeploymentWellKnownProvider.ContentPath,
"yourIISWebsiteName" + "/fileToDelete.txt",
destBaseOptions);
var results = deploymentObject.SyncTo(deployBaseOptions, syncOptions);
奇怪的是,results
即使只有一个文件,它也总是显示 3 个文件被删除......?!