using System.IO;
Move file from Root Folder to Sub Folder
string fileName = "test.txt";
string sourcePath = @"C:\Test
Folder";
string targetPath = @"C:\Test
Folder\Sub Folder";
string sourceFile = Path.Combine(sourcePath,
fileName);
string destFile = Path.Combine(targetPath,
fileName);
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
File.Move(sourceFile,
destFile);
Move file from one folder to another folder
string fileName = "test.txt";
string sourcePath = @"C:\Source
Folder";
string targetPath = @"C:\Destination
Folder";
string sourceFile = Path.Combine(sourcePath,
fileName);
string destFile = Path.Combine(targetPath,
fileName);
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
File.Move(sourceFile,
destFile);
Move file from one directories to another directories
string fileName = "test.txt";
string sourcePath = @"C:\Source
Folder";
string targetPath = @"D:\Destination
Folder";
string sourceFile = Path.Combine(sourcePath,
fileName);
string destFile = Path.Combine(targetPath,
fileName);
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
File.Move(sourceFile,
destFile);
Move all files from Root Folder to Sub Folder
string sourcePath = @"C:\Test
Folder";
string targetPath = @"C:\Test
Folder\Sub Folder";
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
string[] sourcefiles = Directory.GetFiles(sourcePath);
foreach (string sourcefile in sourcefiles)
{
string fileName
= Path.GetFileName(sourcefile);
string destFile
= Path.Combine(targetPath, fileName);
File.Move(sourcefile,
destFile);
}
Move all files from one folder to another folder
string sourcePath = @"C:\Source
Folder";
string targetPath = @"C:\Destination
Folder";
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
string[] sourcefiles = Directory.GetFiles(sourcePath);
foreach (string sourcefile in sourcefiles)
{
string fileName
= Path.GetFileName(sourcefile);
string destFile
= Path.Combine(targetPath, fileName);
File.Move(sourcefile,
destFile);
}
Move all files from one directories to another directories
string sourcePath = @"C:\Source
Folder";
string targetPath = @"D:\Destination
Folder";
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
string[] sourcefiles = Directory.GetFiles(sourcePath);
foreach (string sourcefile in sourcefiles)
{
string fileName
= Path.GetFileName(sourcefile);
string destFile
= Path.Combine(targetPath, fileName);
File.Move(sourcefile,
destFile);
}
Note:-
While moving files, if file already exists in
destination folder. It’s throw error.
It's very helpful. Thanks
ReplyDeleteI found exception-
ReplyDeleteAn exception of type 'System.IO.IOException' occurred in mscorlib.dll but was not handled in user code
Additional information: The process cannot access the file because it is being used by another process.
Make sure file is not opened or you can check your task manager. If file opened the file appear in the task manager list, if so kill file task. i hope this solve issue.
DeleteThanks a lot.......
ReplyDeleteThis comment has been removed by a blog administrator.
ReplyDeleteVery nice and helpful article.
ReplyDelete