C# 复制本地文件夹粘贴到共享文件夹
时间: 2023-08-10 16:04:17 浏览: 534
你可以使用C#中的System.IO和System.Net命名空间来实现复制本地文件夹到共享文件夹的功能。以下是实现的基本步骤:
1. 使用Directory.GetFiles和Directory.GetDirectories方法获取本地文件夹中的文件和子文件夹。
2. 使用File.Copy方法将文件从本地文件夹复制到共享文件夹。
3. 使用Directory.CreateDirectory方法在共享文件夹中创建子文件夹。
4. 递归调用自己,将子文件夹中的文件和文件夹复制到共享文件夹中。
以下是一个示例代码,可以将本地文件夹D:\LocalFolder复制到共享文件夹\\Server\SharedFolder:
```csharp
using System.IO;
using System.Net;
public void CopyFolderToSharedFolder()
{
string localFolderPath = @"D:\LocalFolder";
string sharedFolderPath = @"\\Server\SharedFolder";
// Create a WebRequest instance for the shared folder
WebRequest sharedFolderRequest = WebRequest.Create(sharedFolderPath);
NetworkCredential credentials = new NetworkCredential("username", "password");
sharedFolderRequest.Credentials = credentials;
// Get the response from the shared folder request
WebResponse sharedFolderResponse = sharedFolderRequest.GetResponse();
// Get the shared folder stream and create a StreamWriter instance
Stream sharedFolderStream = sharedFolderResponse.GetResponseStream();
StreamWriter sharedFolderWriter = new StreamWriter(sharedFolderStream);
// Get the files and directories in the local folder
string[] files = Directory.GetFiles(localFolderPath);
string[] directories = Directory.GetDirectories(localFolderPath);
// Copy the files to the shared folder
foreach (string file in files)
{
// Get the filename from the file path
string filename = Path.GetFileName(file);
// Create the shared file path
string sharedFilePath = Path.Combine(sharedFolderPath, filename);
// Copy the file to the shared folder
File.Copy(file, sharedFilePath);
}
// Copy the directories to the shared folder
foreach (string directory in directories)
{
// Get the directory name from the directory path
string directoryName = Path.GetFileName(directory);
// Create the shared directory path
string sharedDirectoryPath = Path.Combine(sharedFolderPath, directoryName);
// Create the shared directory
Directory.CreateDirectory(sharedDirectoryPath);
// Recursively copy the files and directories in the local directory to the shared directory
CopyFolder(directory, sharedDirectoryPath);
}
// Close the shared folder stream and response
sharedFolderWriter.Close();
sharedFolderResponse.Close();
}
private void CopyFolder(string sourceFolderPath, string targetFolderPath)
{
// Get the files and directories in the source folder
string[] files = Directory.GetFiles(sourceFolderPath);
string[] directories = Directory.GetDirectories(sourceFolderPath);
// Copy the files to the target folder
foreach (string file in files)
{
// Get the filename from the file path
string filename = Path.GetFileName(file);
// Create the target file path
string targetFilePath = Path.Combine(targetFolderPath, filename);
// Copy the file to the target folder
File.Copy(file, targetFilePath);
}
// Copy the directories to the target folder
foreach (string directory in directories)
{
// Get the directory name from the directory path
string directoryName = Path.GetFileName(directory);
// Create the target directory path
string targetDirectoryPath = Path.Combine(targetFolderPath, directoryName);
// Create the target directory
Directory.CreateDirectory(targetDirectoryPath);
// Recursively copy the files and directories in the source directory to the target directory
CopyFolder(directory, targetDirectoryPath);
}
}
```
在上面的代码中,我们首先使用WebRequest和NetworkCredential类设置访问共享文件夹所需的凭据。然后,我们使用Directory.GetFiles和Directory.GetDirectories方法获取本地文件夹中的文件和子文件夹,并使用File.Copy方法将文件从本地文件夹复制到共享文件夹。接下来,我们使用Directory.CreateDirectory方法在共享文件夹中创建子文件夹,并递归调用自己,将子文件夹中的文件和文件夹复制到共享文件夹中。最后,我们关闭共享文件夹流和响应。
阅读全文