将文件复制到指定路径[C# 文件操作]
2010-03-10 15:07
162 查看
将现有文件复制到新文件,不允许改写现有文件。[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.");
}
}
}
若要执行此操作... 请参见本主题中的示例...
创建文本文件。 向文件写入文本
写入文本文件。 向文件写入文本
读取文本文件。 从文件读取文本
向文件中追加文本。 打开并附加到日志文件
File.AppendText
FileInfo.AppendText
重命名或移动文件。 File.Move
FileInfo.MoveTo
删除文件。 File.Delete
FileInfo.Delete
复制文件。 File.Copy
FileInfo.CopyTo
获取文件大小。 FileInfo.Length
获取文件属性。 File.GetAttributes
设置文件属性。 File.SetAttributes
确定文件是否存在。 File.Exists
读取二进制文件。 对刚创建的数据文件进行读取和写入
写入二进制文件。 对刚创建的数据文件进行读取和写入
检索文件扩展名。 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();
}
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/CRSUN/archive/2005/01/01/236726.aspx
将现有文件复制到新文件,允许改写现有文件。[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.");
}
}
}
若要执行此操作... 请参见本主题中的示例...
创建文本文件。 向文件写入文本
写入文本文件。 向文件写入文本
读取文本文件。 从文件读取文本
向文件中追加文本。 打开并附加到日志文件
File.AppendText
FileInfo.AppendText
重命名或移动文件。 File.Move
FileInfo.MoveTo
删除文件。 File.Delete
FileInfo.Delete
复制文件。 File.Copy
FileInfo.CopyTo
获取文件大小。 FileInfo.Length
获取文件属性。 File.GetAttributes
设置文件属性。 File.SetAttributes
确定文件是否存在。 File.Exists
读取二进制文件。 对刚创建的数据文件进行读取和写入
写入二进制文件。 对刚创建的数据文件进行读取和写入
检索文件扩展名。 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();
}
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/CRSUN/archive/2005/01/01/236726.aspx
相关文章推荐
- 将文件复制到指定路径[C# 文件操作]
- c#net无法将文件 复制到 对路径访问被拒绝或源文件 未能打开(未指定的错误)
- c#将一个文件夹及其里面的所有内容(文件和文件夹)复制到指定路径
- C#实现把指定文件夹下的所有文件复制到指定路径下以及修改指定文件的后缀名
- c#如何实现对指定路径文件夹的遍历并复制里面所有文件到指定路径
- C#路径,文件,目录,I/O常见操作汇总
- C# 复制指定文件夹下的所有文件和文件夹
- 完成文件复制操作,在程序运行后提示输入源文件路径,然后再输入目标文件路径。
- (C#)路径/文件/目录/I/O常见操作汇总
- C#中File类操作文件实例-创建文件、复制文件、移动文件
- C#将制定文件夹下的PDF文件合并成一个并输出至指定路径
- C#路径/文件/目录/I/O常见操作汇总<转载>
- C#打开指定目录,并将焦点放在指定文件上。相对路径(程序起动的目录)
- C# 对包含文件或目录路径信息的 System.String 实例执行操作
- C#路径/文件/目录/I/O常见操作汇总(上)
- C#路径/文件/目录/I/O常见操作汇总
- 简单的C# 保存内容到指定路径下的XML文件中方法
- 编写文件搜索小程序:1. 输入绝对路径以及搜索关键字,2. 搜索指定路径下(包括子文件夹)中名称包含关键字的所有文件并打印出,3. 将当前操作记录日志
- C# 打开指定路径下文件
- C# 路径/文件/目录/I/O常见操作汇总收藏