将文件从一个驱动器复制到另一个驱
问题描述:
您好b $ b
我想将一组以单一格式命名的文件迁移到另一个驱动器中的某个位置。
例如,如果我有一组图像,以包含名称和日期的格式命名,如:DAME中的NAME_DDMMYY //我希望将它们迁移到E://。,我该怎么做?
我想在.net framework 4.0中使用C#。
Hi
I'm looking to migrate a set of files named in a single format to a location in another drive.
For eg., if I have a set of images, named in a format that contains name and date like:NAME_DDMMYY in a D:// and I want to migrate them to E://., how can I do that?
I would like to use C# with .net framework 4.0.
答
下面的代码将帮助你............
Belowing Code Will Help you............
Private Sub btnBackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBackup.Click
Dim sourcepath As String = "C:\temp\test\1.txt"
Dim DestPath As String = "D:\Data\"
CopyDirectory(sourcepath, DestPath)
End Sub
Private Shared Sub CopyDirectory(sourcePath As String, destPath As String)
If Not Directory.Exists(destPath) Then
Directory.CreateDirectory(destPath)
End If
For Each file__1 As String In Directory.GetFiles(sourcePath)
Dim dest As String = Path.Combine(destPath, Path.GetFileName(file__1))
File.Copy(file__1, dest)
Next
For Each folder As String In Directory.GetDirectories(sourcePath)
Dim dest As String = Path.Combine(destPath, Path.GetFileName(folder))
CopyDirectory(folder, dest)
Next
End Sub
string[] files = Directory.GetFiles(@"Z:\", "*.bak");foreach (string file in files)
{
File.Copy(file, Path.Combine(@"E:\", Path.GetFileName(file)) , true);
}
希望这可以帮到你。
Hope this helps you.
试试这个代码。
请阅读提问的指南。
在此处发布问题之前进行最低限度的搜索。
Try this code.
Please read the guidelines for asking a question.
Do a minimum search, before posting a question here.
public class SimpleFileCopy
{
static void Main()
{
string fileName = "test.txt";
string sourcePath = @"C:\Users\Public\TestFolder";
string targetPath = @"C:\Users\Public\TestFolder\SubDir";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
// To copy a file to another location and
// overwrite the destination file if it already exists.
System.IO.File.Copy(sourceFile, destFile, true);
// To move a file or folder to a new location:
System.IO.File.Move(sourceFile, destinationFile);
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);
foreach (string s in files)
{
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile, true);
}
}
else
{
Console.WriteLine("Source path does not exist!");
}
}
}