gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
C# 下载文件夹 并带目录结构
- User
C# 下载文件夹 并带目录结构
- Assistant
您可以使用以下代码下载整个文件夹,包括子文件夹和文件,并且保留目录结构。
using System; using System.IO; using System.Net; class Program { static void Main(string[] args) { string sourceUrl = "https://example.com/folder"; // 文件夹的 URL string destinationPath = @"C:\Downloads\folder"; // 目标文件夹的路径 DownloadFolder(sourceUrl, destinationPath); } static void DownloadFolder(string sourceUrl, string destinationPath) { WebClient client = new WebClient(); client.DownloadFile(sourceUrl + "/.ignore", Path.Combine(destinationPath, ".ignore")); // 创建目标文件夹 DownloadFolderRecursively(sourceUrl, destinationPath, client); } static void DownloadFolderRecursively(string sourceUrl, string destinationPath, WebClient client) { string pageSource = client.DownloadString(sourceUrl); // 获取页面源代码 string[] lines = pageSource.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); // 将源代码分割成行 foreach (string line in lines) { if (line.Contains("<a href=")) // 如果该行是一个链接 { int startIndex = line.IndexOf("\"") + 1; int endIndex = line.IndexOf("\"", startIndex); string link = line.Substring(startIndex, endIndex - startIndex); // 提取链接 string fileUrl = sourceUrl + "/" + link; if (link.EndsWith("/")) { string folderName = link.Substring(0, link.Length - 1); string folderPath = Path.Combine(destinationPath, folderName); Directory.CreateDirectory(folderPath); // 创建子文件夹 DownloadFolderRecursively(fileUrl, folderPath, client); // 递归下载子文件夹 } else { string filePath = Path.Combine(destinationPath, link); client.DownloadFile(fileUrl, filePath); // 下载文件 } } } } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.