我找不到使用winscp .net汇编的好WebDav实现示例。
特别是我正在寻找一些将文件推向WebDav服务器的代码。
有人可以提供一些示例C#代码以帮助我开始吗?
这是我最终以。
的winscp .NET WebDav文件推动实现的示例。不是很漂亮,但似乎可以完成工作。
希望这能为别人节省一个令人沮丧的几个小时。
快速注意:webdavuploadsettings.default.xxxx是我访问我所有连接信息的设置文件的方式。
public static void SendFileWebDav(string FileToSend)
{
if (!DoesFileExist(FileToSend))
{
throw new Exception(string.Format("Cannot Find File {0}", FileToSend));
}
SessionOptions sessionOptions = new SessionOptions();
sessionOptions.UserName = WebDavUploadSettings.Default.Username;
sessionOptions.Password = WebDavUploadSettings.Default.Password;
if (string.IsNullOrWhiteSpace(WebDavUploadSettings.Default.FolderPathToPushTo)) //WINSCP will throw an error if root directory doesn't start with /
{
//Haven't tested this.
sessionOptions.WebdavRoot = "/";
}
else
{
sessionOptions.WebdavRoot = WebDavUploadSettings.Default.FolderPathToPushTo.ToString(); //Added .ToString() here because I was getting 404 error other wise? Maybe because is was not reading in the setting correctly.
}
//Make sure you define this as a URL without protocal. (Leave off Https:// and Http://)
sessionOptions.HostName = WebDavUploadSettings.Default.Website;
sessionOptions.Protocol = Protocol.Webdav;
Session WebDavSession = new Session();
try
{
WebDavSession.Open(sessionOptions);
TransferOptions TransferOptions = new TransferOptions();
TransferOptions.TransferMode = TransferMode.Automatic;
TransferOperationResult transferResult;
transferResult = WebDavSession.PutFiles(FileToSend,Path.GetFileName(FileToSend), false, TransferOptions);
transferResult.Check();
}
catch (Exception e)
{
throw e;
}
finally
{
WebDavSession.Close();
}
}