using System;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace copy_file1
{
class Program
{
static void Main(string[] args)
{
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());
} }
}
}
using System.Collections.Generic;
using System.Text;
using System.IO;namespace copy_file1
{
class Program
{
static void Main(string[] args)
{
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());
} }
}
}
解决方案 »
- "WorkFlow Fuondation"与"自动化控制"的结合讨论
- 委托事件的问题 委托的对象为null 郁闷
- 关于System.Data.OracleClient的问题
- .NET2003 打包的问题 急!!!(Online Waiting)
- 请大家畅谈一下"多对多通信"的设计思路
- 怎样在.net dll程序集中输出函数供非托管C++等语言调用?
- 指定默认打印机时出错,请教各位高人?
- c# 配置文件不能解析&字符
- 名称不能以“<”字符(十六进制值 0x3C)开头
- dataset.GetChanges()为什么不可以得到删除了的记录???
- C#安装部署,请问在系统必备里面,怎样把.net Framework2.0 SP1补丁也包括在里面呢?
- 如何使整数后面加0
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace copy_file1
{
class Program
{
static void Main(string[] args)
{
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());
} }
}
}
using (FileStream fs = File.Create(path)) { }
// Ensure that the target does not exist.
File.Delete(path2); 这里用Create的形式OpenStream,path为源文件,就变为空文件了。
3楼的办法可以,我也是这样试出来的。但是不知道为什么要注释掉这一行。
是重新创建了那个path的文件,是吗?