`
happmaoo
  • 浏览: 4340981 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

将文件复制到指定路径[C# 文件操作]

阅读更多

将现有文件复制到新文件,不允许改写现有文件。[C#] public FileInfo CopyTo(string);

将现有文件复制到新文件,允许改写现有文件。[C#] public FileInfo CopyTo(string, bool);
[C#]

将文件复制到指定路径,允许改写同名的目标文件 COPY
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.");
}
}
}
[C#] COPY TO
using System;
using System.IO;

class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
string path2 = @"c:\temp\MyTest.txt" + "temp";
FileInfo fi1 = new FileInfo(path);
FileInfo fi2 = new FileInfo(path2);

try
{
// Create the file and clean up handles.
using (FileStream fs = fi1.Create()) {}

//Ensure that the target does not exist.
fi2.Delete();

//Copy the file.
fi1.CopyTo(path2);
Console.WriteLine("{0} was copied to {1}.", path, path2);

//Try to copy it again, which should succeed.
fi1.CopyTo(path2, true);

Console.WriteLine("The second Copy operation succeeded, which is expected.");

}
catch
{
Console.WriteLine("Double copying was not allowed, which is not expected.");
}
}
}




若要执行此操作... 请参见本主题中的示例...
创建文本文件。 <?XML:NAMESPACE PREFIX = MSHELP /><link tabindex="0" keywords="cpconwritingtexttofile">
写入文本文件。 <link tabindex="0" keywords="cpconwritingtexttofile">
读取文本文件。 <link tabindex="0" keywords="cpconreadingtextfromfile">
向文件中追加文本。 <link tabindex="0" keywords="cpconopeningappendingtologfile">

File.AppendText

FileInfo.AppendText

重命名或移动文件。 File.Move

FileInfo.MoveTo

删除文件。 File.Delete

FileInfo.Delete

复制文件。 File.Copy

FileInfo.CopyTo

获取文件大小。 FileInfo.Length
获取文件属性。 File.GetAttributes
设置文件属性。 File.SetAttributes
确定文件是否存在。 File.Exists
读取二进制文件。 <link tabindex="0" keywords="cpconReadingWritingToNewlyCreatedDataFile">
写入二进制文件。 <link tabindex="0" keywords="cpconReadingWritingToNewlyCreatedDataFile">
检索文件扩展名。 Path.GetExtension
检索文件的完全限定路径。 Path.GetFullPath
检索路径中的文件名和扩展名。 Path.GetFileName
更改文件扩展名。 Path.ChangeExtension


Progress 类的使用
private void CopyWithProgress(string[] filenames)
{
// Display the ProgressBar control.
pBar1.Visible = true;
// Set Minimum to 1 to represent the first file being copied.
pBar1.Minimum = 1;
// Set Maximum to the total number of files to copy.
pBar1.Maximum = filenames.Length;
// Set the initial value of the ProgressBar.
pBar1.Value = 1;
// Set the Step property to a value of 1 to represent each file

being copied.
pBar1.Step = 1;
// Loop through all files to copy.
for (int x = 1; x <= filenames.Length; x++)
{
// Copy the file and increment the ProgressBar if successful.
if(CopyFile(filenames[x-1]) == true)
{
// Perform the increment on the ProgressBar.
pBar1.PerformStep();
}
}
}

分享到:
评论

相关推荐

    C#复制删除指定路径下的文件,操作

    这是我封装的一个将指定路径的文件夹下的所以文件复制和删除的类,拿过去可以直接用!

    C# winfrom 批量查询文件并复制到目标位置

    批量导出文件,选择目标文件夹(能循环往下查所有子文件夹),查找出所有含有该名称的文件(后缀可以是jpg,png,log等),并复制到指定路径

    C# 上传下载附件方法

    根据时间获取指定路径的 后缀名的 的所有文件, 复制文件夹,检查文件, 如果文件不存在则创建, 删除指定文件夹对应其他文件夹里的文件, 从文件的绝对路径中获取文件名( 包含扩展名 ), 复制文件参考方法,页面中引用,...

    C#制作的windows系统文件快速搜索工具,读取USN,易用性与速度都已优化的很好。程序为免安装的exe文件。

    1、列表文件支持批量处理(删除、复制、复制文件名路径、打开、重命名),或者引用系统菜单。 2、支持拼音首字母缩写搜索,指定文件夹内搜索,多关键词搜索。 3、支持大部分文件打开、保存对话框的搜索定位。 4、...

    LISTVIEW显示指定目录文件

    实现文件的显示 指定的目录 点击复制 点击修改 点击删除

    C#实现复制文件夹中文件到另一个文件夹的方法

    本文实例讲述了C#实现复制文件夹中文件到另一个文件夹的方法。分享给大家供大家参考。具体如下: private void CopyDir(string srcPath, string aimPath) {  try  {  // 检查目标目录是否以目录分割字符结束如果...

    定时自动删除指定文件夹下文件的Winform应用程序

    ③可以根据系统时间结合(输入删除天数)来进行周期删除指定文件夹下的文件(比如:可以将输入天数设置为90天,且选择系统当前时间则表示删除当前时间下90天以前的文件,该功能用作定期清除日志文件场景十分适合...

    C#根据文件大小显示文件复制进度条

    根据文件大小显示文件复制进度条,操作步骤:C#实例化FileStream类,打开文件对话框,获取源文件的路径,获取目的文件的路径,创建一个线程,在线程上执行指定的委托,对文件进行复制,并在复制完成后关闭线程,设置...

    C#程序开发范例宝典

     6.6 指定类型的文件操作 270  实例188 文本文件的操作 270  实例189 简单的文件加密解密 271  6.7 其他 273  实例190 获取窗口文本 273  实例191 判断文件是否正在被使用 274  实例192 ...

    C# 复制文件夹内容

    C#复制指定文件夹内容到指定路径的demo,包括完整的程序演示。复制可用循环复制文件夹中的多层文件的内容

    C#开发经验技巧宝典

    0711 将图片保存到XML文件中 440 0712 如何比较两个文件的内容是否相同 440 0713 如何以缩略图形式上传图片文件 441 0714 如何解析只有一种格式的文本文件 442 0715 如何解析含有多种格式的文本文件 443 ...

    C#开发实例大全(基础卷).软件开发技术联盟(带详细书签) PDF 下载

    主要内容有C#开发环境的使用、C#语言基础应用、字符串处理技术、数组和集合的使用、面向对象编程...文件、C#与Word互操作、高效应用Excel、基本图形绘制、图像处理技术、常用图表应用、动画处理技术、音频与视频控制...

    更新程序前自动备份文件---将一个目录下的所有文件复制到指定目录Source.rar

    C#源码-更新程序前自动备份文件 包含了源码,也包含了使用文档,可以不看源码直接使用,简单易用 用于更新程序前自动备份文件,当更新出问题后,用于回退版本 1.修改指向路径可以自动更新程序,或者用户自动回退版本

    C#文件拷贝工具(只拷贝文件,不拷贝存储路径)

    只拷贝指定文件后缀的东西到新的文件夹里面,不管你原来的文件夹里面都多少个子文件夹都能给你把需要的文件复制出来(但是不会复制子文件夹,即不会复制原来的存储结构,我特意这么做的)! 你上次选的这四个选项,...

    C#基础类库

    CSV文件导入DataTable和DataTable导出到Csv文件等操作 4.DEncrypt 加密/解密帮助类 DEncrypt C#DEncrypt加密/DEncrypt解密帮助类 ,多种方式,可以设置Key DESEncrypt C#DESEncrypt加密/DESEncrypt解密帮助类 ...

    C#全能速查宝典

    《C#全能速查宝典》共分为8章,分别介绍了C#语言基础、Windows窗体及常用控件、Windows高级控件、控件公共属性、方法及事件、数据库开发、文件、数据流与注册表、GDI+绘图技术和C#高级编程,共包含562个C#编程中常用...

    C#基类库(苏飞版)

    CSV文件导入DataTable和DataTable导出到Csv文件等操作 4.DEncrypt 加密/解密帮助类 DEncrypt C#DEncrypt加密/DEncrypt解密帮助类 ,多种方式,可以设置Key DESEncrypt C#DESEncrypt加密/DESEncrypt解密帮助类 ...

Global site tag (gtag.js) - Google Analytics