要在C#項目中集成WebDAV服務,您可以使用第三方庫,例如WebDAVClient或IT Hit WebDAV Server Engine
Install-Package WebDAVClient -Version 1.0.0
using System;
using System.Net;
using System.IO;
using WebDAVClient;
string serverUrl = "https://yourserver.com/webdav";
string username = "your_username";
string password = "your_password";
WebDAVClient.Client webDavClient = new WebDAVClient.Client(new Uri(serverUrl));
NetworkCredential credentials = new NetworkCredential(username, password);
webDavClient.Credentials = credentials;
// 列出目錄內(nèi)容
Uri directoryUri = new Uri(serverUrl + "/directory");
WebDAVClient.Model.Resource[] resources = await webDavClient.Propfind(directoryUri);
foreach (var resource in resources)
{
Console.WriteLine($"{resource.Uri} - {resource.IsCollection}");
}
// 上傳文件
Uri fileUri = new Uri(serverUrl + "/file.txt");
string localFilePath = @"C:\local\path\to\file.txt";
using (Stream localFileStream = File.OpenRead(localFilePath))
{
await webDavClient.Put(fileUri, localFileStream);
}
// 下載文件
Uri remoteFileUri = new Uri(serverUrl + "/remotefile.txt");
string localDownloadPath = @"C:\local\path\to\downloadedfile.txt";
using (Stream remoteFileStream = await webDavClient.Get(remoteFileUri))
using (Stream localFileStream = File.Create(localDownloadPath))
{
await remoteFileStream.CopyToAsync(localFileStream);
}
這只是一個簡單的示例,您可以根據(jù)需要修改代碼以適應您的項目。有關更多詳細信息和其他功能,請參閱WebDAVClient庫的文檔。