File.Copy的方法是: 将现有文件复制到新文件。不允许改写同名的文件。 上面是MSDN定义的解释 不允许改同名文件的意思是,你复制过去的文件不能有相同的名称的文件。 否则将跳出IOException异常。 这个错误表示:文件已经存在或System.IO异常 这个函数的所带的两个参数,都为绝对路径。 一个为要复制的文件的路径 一个为目标文件的路径 在使用这个方法时候要注意它可能抛出的几个异常。 上面提到的那个异常是其中的一个。
- JS倒计时代码大全
- 如何优化网站内容?
- thinkphp框架的特点
- 关键字也有权重吗?
- .NET/C#中 System.Text.Encoding 类的一个简单用法
- access iif语句
- 自媒体包含了哪些平台?
- PHP中::、->、self、$this几种操作符的区别介绍
- c#中访问修饰符public,private,protected,internal,protected internal的区别
- SEO是什么意思,SEO优化大致的步骤有那些?
邮箱:
手机:15383239821
File.Copy 方法
| 名称 | 说明 |
|---|---|
| File.Copy (String, String) | 将现有文件复制到新文件。不允许改写同名的文件。
由 .NET Compact Framework 支持。 |
| File.Copy (String, String, Boolean) | 将现有文件复制到新文件。允许改写同名的文件。
由 .NET Compact Framework 支持。 |
File.Copy (String, String)
将现有文件复制到新文件。不允许改写同名的文件。public static void Copy ( string sourceFileName, string destFileName )参数
- sourceFileName
要复制的文件。
using System; using System.IO; class Test { public static void Main() { 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()); } } }
- destFileName
目标文件的名称。它不能是一个目录或现有文件。
File.Copy (String, String, Boolean)
将现有文件复制到新文件。允许改写同名的文件。
参数
- sourceFileName
-
要复制的文件。
- destFileName
-
目标文件的名称。不能是目录。
- overwrite
-
如果可以改写目标文件,则为 true;否则为 false。
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
string path2 = path + "temp";
try { // Create the file and clean up handles.
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 succeed.
File.Copy(path, path2, true);
Console.WriteLine("The second Copy operation succeeded, which was expected.");
}
catch
{
Console.WriteLine("Double copy is not allowed, which was not expected.");
}
}
}
public static void Copy (
string sourceFileName,
string destFileName,
bool overwrite
)
- 上一篇:ASPNET: 请确保此代码文件中定义的类与“inherits”属性匹配,并且该类扩展的基类(例如 Page 或 UserControl)是正确的
- 下一篇:关于FileUpload文件上传的文件大小限制问题
-
2010-11-11如何创造伪原创,巧妙修改文章标题
-
2013-10-24AppSettings和ConnectionStrings的区别
-
2023-03-01做APP需要什么资质?
-
2010-12-09什么是死链接
-
2012-07-02如何优化指数在1000以上的关键词
-
2023-12-06blockquote标签
