使用c#.net将文件从一个文件夹剪切并粘贴到另一个文件夹 [英] Cut and paste the files from one folder to another folder using c#.net
本文介绍了使用c#.net将文件从一个文件夹剪切并粘贴到另一个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用C#
推荐答案
将文件从一个文件夹剪切并粘贴到另一个文件夹。剪切和粘贴在这里完全没有意义。它只是意味着移动一些文件。请参阅: http://msdn.microsoft.com/en-us/library /system.io.file.move.aspx [ ^ ]。
-SA
"Cut and paste" makes no sense at all here. It simply means moving some files. Please see: http://msdn.microsoft.com/en-us/library/system.io.file.move.aspx[^].
—SA
public class SimpleFileCopy
{
static void Main()
{
string fileName = "test.txt";
string sourcePath = @"C:\Users\Public\TestFolder";
string targetPath = @"C:\Users\Public\TestFolder\SubDir";
// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
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 copy all the files in one directory to another directory.
// Get the files in the source folder. (To recursively iterate through
// all subfolders under the current directory, see
// "How to: Iterate Through a Directory Tree.")
// Note: Check for target path was performed previously
// in this code example.
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);
// Copy the files and overwrite destination files if they already exist.
foreach (string s in files)
{
// Use static Path methods to extract only the file name from the path.
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!");
}
// Keep console window open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
这会将所有文件从一个文件夹复制到另一个文件夹
this will copy all file from one folder to an other
// To move a file or folder to a new location:
System.IO.File.Move(sourceFilePathwithFileName, destinationFilePathwithFileName);
// To move an entire directory. To programmaticly modify or combine
// path strings, use the System.IO.Path class.
System.IO.Directory.Move(@"C:\Users\Public\public\test\", @"C:\Users\Public\private");
.Net框架中有内置函数用于System.IO命名空间内的这个目的
There is inbuilt function in .Net framework for this purpose inside System.IO namespace
File.Move(sourceFilePath, destnationFilePath);
这篇关于使用c#.net将文件从一个文件夹剪切并粘贴到另一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文