Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将文件复制到目录_C#_.net_File_Copy - Fatal编程技术网

C# 将文件复制到目录

C# 将文件复制到目录,c#,.net,file,copy,C#,.net,File,Copy,将文件复制到目录时是否没有.NET库调用?我找到的所有库调用(例如File.Copy()或FileInfo.CopyTo())仅支持将文件复制到另一个完全指定的文件 有图书馆电话吗?如果没有,那么编写我自己的实用程序的最佳方法是什么?我真的必须使用Path.GetFileName()?试试这个例子 public class SimpleFileCopy { static void Main() { string fileName = "test.txt"; string so

将文件复制到目录时是否没有.NET库调用?我找到的所有库调用(例如
File.Copy()
FileInfo.CopyTo()
)仅支持将文件复制到另一个完全指定的文件

有图书馆电话吗?如果没有,那么编写我自己的实用程序的最佳方法是什么?我真的必须使用
Path.GetFileName()

试试这个例子

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);      
    System.IO.File.Copy(sourceFile, destFile, true);      

}
}

试试这个例子

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);      
    System.IO.File.Copy(sourceFile, destFile, true);      

}
}

我真的必须使用Path.GetFileName()吗

确切地说:

string destination = Path.Combine(dir, Path.GetFileName(file));
Directory.CreateDirectory(dir);
File.Copy(file, destination);
我真的必须使用Path.GetFileName()吗

确切地说:

string destination = Path.Combine(dir, Path.GetFileName(file));
Directory.CreateDirectory(dir);
File.Copy(file, destination);

为什么要避免Path.GetFileName()?我只是希望在.NET framework中已经有这样的库调用。这样的方法可能有用,但我想指定文件名是一件好事。不过,如果只想隐藏intant,可以创建一个扩展方法。这就完成了获取文件名等所有工作。我可以为您编写一个吗?这不是扩展方法。为什么要避免Path.GetFileName()?我只是希望在.NET framework中已经有这样的库调用。这样的方法可能有用,但我想指定文件名是一件好事。不过,如果只是想隐藏intant,可以创建一个扩展方法。这就完成了获取文件名等的所有工作。我可以为您写一个吗?这不是扩展方法。好的,这正是我所拥有的,很遗憾没有其他东西,无论如何,谢谢。(尽快接受答案,8分钟…)好的,这也正是我所拥有的,遗憾的是没有其他东西,无论如何谢谢。(尽快接受答案,8分钟…)