MSDeploy API - 通过代码删除远程文件

时间:2012-10-23 10:14:25

标签: msdeploy webdeploy

我希望通过c#代码使用MSDeploy API远程触发删除命令。

我想要实现以下命令:

msdeploy.exe -verb:delete -dest:contentPath="/folderName/filename.txt"

而不是通过运行非托管外部可执行文件,我想使用MSDeploy .Net API执行此操作。

2 个答案:

答案 0 :(得分:2)

假设您正在尝试删除绝对文件路径(而不是网站中的文件),那么您正在寻找类似这样的内容:

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

答案 1 :(得分:0)

我已经找到了答案,感谢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个文件被删除,即使只有一个...?!