C# 如何将(过滤后的)文件列表传递给 WebDav.Client 并上传该列表中的所有文件?现在我只能放单个文件
C# how can I pass a (filtered) file list to WebDav.Client and upload all files on that list? Right now I can only put individual files
我正在创建一个 webdav.client c# 应用程序,它将扫描选定目录中的特定文件类型。然后获取找到的文件列表并将它们提供给 WebDav 客户端进行上传。现在我只能上传单个文件。
public async void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.RootFolder = Environment.SpecialFolder.Personal;
if (dialog.ShowDialog() == DialogResult.OK)
{
var files = Directory.GetFiles(dialog.SelectedPath).Where(file => Regex.IsMatch(file, @"^.+\.(txt|jpg|png)$"));
{
var clientParams = new WebDavClientParams { BaseAddress = new Uri("https://myserver.com"), Credentials = new NetworkCredential("username", "password") };
using (var client = new WebDavClient(clientParams))
{
await client.Mkcol(textBox2.Text+textBox1.Text); // create a directory
await client.PutFile("test.text", File.OpenRead("text.text")); // upload a resource
}
}
}
}
只需遍历此文件列表并上传每个文件:
var files = Directory.GetFiles(dialog.SelectedPath).Where(file => Regex.IsMatch(file, @ "^.+\.(txt|jpg|png)$"));
foreach(var file in files) {
var clientParams = new WebDavClientParams {
BaseAddress = new Uri("https://myserver.com"), Credentials = new NetworkCredential("username", "password")
};
using(var client = new WebDavClient(clientParams)) {
await client.Mkcol(textBox2.Text + textBox1.Text); // create a directory
await client.PutFile(file, File.OpenRead(file)); // upload a resource
}
}
我正在创建一个 webdav.client c# 应用程序,它将扫描选定目录中的特定文件类型。然后获取找到的文件列表并将它们提供给 WebDav 客户端进行上传。现在我只能上传单个文件。
public async void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.RootFolder = Environment.SpecialFolder.Personal;
if (dialog.ShowDialog() == DialogResult.OK)
{
var files = Directory.GetFiles(dialog.SelectedPath).Where(file => Regex.IsMatch(file, @"^.+\.(txt|jpg|png)$"));
{
var clientParams = new WebDavClientParams { BaseAddress = new Uri("https://myserver.com"), Credentials = new NetworkCredential("username", "password") };
using (var client = new WebDavClient(clientParams))
{
await client.Mkcol(textBox2.Text+textBox1.Text); // create a directory
await client.PutFile("test.text", File.OpenRead("text.text")); // upload a resource
}
}
}
}
只需遍历此文件列表并上传每个文件:
var files = Directory.GetFiles(dialog.SelectedPath).Where(file => Regex.IsMatch(file, @ "^.+\.(txt|jpg|png)$"));
foreach(var file in files) {
var clientParams = new WebDavClientParams {
BaseAddress = new Uri("https://myserver.com"), Credentials = new NetworkCredential("username", "password")
};
using(var client = new WebDavClient(clientParams)) {
await client.Mkcol(textBox2.Text + textBox1.Text); // create a directory
await client.PutFile(file, File.OpenRead(file)); // upload a resource
}
}