首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c#net 应用程序:怎么拷贝文件到指定文件夹

2012-02-15 
c#.net 应用程序:如何拷贝文件到指定文件夹?c#.net应用程序:如何拷贝文件到指定文件夹?[解决办法]下面的示

c#.net 应用程序:如何拷贝文件到指定文件夹?
c#.net   应用程序:如何拷贝文件到指定文件夹?

[解决办法]
下面的示例将文件复制到指定路径,不允许改写同名的目标文件。

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());
}
}
}
[解决办法]
System.IO.File.Copy即可

热点排行