C#File.Copy方法
假如说我想把C:\Users\Y470\Desktop\新建文本文档.txt复制到C:\Users\Y470\Desktop\新建文件夹里面,怎么写代码啊...
假如说我想把
C:\Users\Y470\Desktop\新建文本文档.txt
复制到
C:\Users\Y470\Desktop\新建文件夹
里面 ,怎么写代码啊 展开
C:\Users\Y470\Desktop\新建文本文档.txt
复制到
C:\Users\Y470\Desktop\新建文件夹
里面 ,怎么写代码啊 展开
3个回答
展开全部
将现有文件复制到新文件。
见msdn:http://msdn.microsoft.com/zh-cn/library/c6cfw35a.aspx
见msdn:http://msdn.microsoft.com/zh-cn/library/c6cfw35a.aspx
追问
不好意思啊,我忘举例子问了
追答
string path1,path2;
path1="C:\Users\Y470\Desktop\新建文本文档.txt ";
path2="C:\Users\Y470\Desktop\新建文件夹 ";
try
{
File.Copy(path1, Path.Combine(path2, Path.GetFileName(path1)); //将path1复制到path2目录下,文件名相同。
//不允许覆盖同名文件,否则会抛出异常。但是,可以使用file.copy的另一个重载版本:
//File.Copy(path1, Path.Combine(path2, Path.GetFileName(path1), true),这就可以覆盖掉同名文件了。
}
catch(Exception)
{
}
展开全部
string path = @"c:\temp\MyTest.txt";
string path2 = path + "temp";
try
{
using (FileStream fs = File.Create(path)) {}
// Ensure that the target does not exist.
File.Delete(path2);
// Copy the file.
File.Copy(path, path2);
Console.WriteLine("{0} copied to {1}", path, path2);
// Try to copy the same file again, which should fail.
File.Copy(path, path2);
Console.WriteLine("The second Copy operation succeeded, which was not expected.");
}
catch (Exception e)
{
Console.WriteLine("Double copying is not allowed, as expected.");
Console.WriteLine(e.ToString());
}
这一段代码, 看懂了吗?
string path2 = path + "temp";
try
{
using (FileStream fs = File.Create(path)) {}
// Ensure that the target does not exist.
File.Delete(path2);
// Copy the file.
File.Copy(path, path2);
Console.WriteLine("{0} copied to {1}", path, path2);
// Try to copy the same file again, which should fail.
File.Copy(path, path2);
Console.WriteLine("The second Copy operation succeeded, which was not expected.");
}
catch (Exception e)
{
Console.WriteLine("Double copying is not allowed, as expected.");
Console.WriteLine(e.ToString());
}
这一段代码, 看懂了吗?
追问
不好意思啊,我忘举例子问了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
string path1,path2;
path1="C:\Users\Y470\Desktop\新建文本文档.txt ";
path2="C:\Users\Y470\Desktop\新建文件夹 ";
File.Copy(path1,path2);
path1="C:\Users\Y470\Desktop\新建文本文档.txt ";
path2="C:\Users\Y470\Desktop\新建文件夹 ";
File.Copy(path1,path2);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |