如何复制文件 [英] How to copy a file

查看:122
本文介绍了如何复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我有问题,请帮助我
我在C盘中有一个.txt文件,它的名称是Hello.txt
现在我想将此文件复制到D盘.使用C#窗口应用程序

首先,不要将您的问题命名为大家好"-它不会告诉我们您的问题是什么.使用如何复制文件?"或类似.

使用 File.Copy [ ^ ]


您好,下面是将文件一个位置复制到另一个位置的代码.希望对您有帮助.

private void button1_Click(对象发送者,EventArgs e)
{
StreamReader objReader =新的StreamReader("C:\\ Hello.txt");
字符串sLine = string.Empty;
ArrayList arrText = new ArrayList();
而(objReader.Peek()== 72)
{
sLine = objReader.ReadLine();
如果(sLine!=")
arrText.Add(sLine);
}
FileInfo fi =新的FileInfo("D:\\ Hello.txt");
//实际创建文件.
FileStream fs = fi.Create();
字节[] fb =
新的UTF8Encoding(true).GetBytes(arrText.ToString());
fs.Write(fb,0,fb.Length);
//根据需要修改文件,然后关闭文件.
fs.Close();
}


Hi Everybody
i have a problem so please help me
i have a one .txt file in C drive and it''s name is Hello.txt
now i want to copy this file to D drive. using C# window application

thanks in Advance

解决方案

Firstly, don''t title your question "Hi Everybody" - it tells us nothing about what your problem is. Use "How to copy a file?" or similar.

Use File.Copy[^]


Hi kikhatri, the below is the code to copy the file one location to another. Hope this would help you.

private void button1_Click(object sender, EventArgs e)
{
StreamReader objReader = new StreamReader("C:\\Hello.txt");
string sLine =string.Empty;
ArrayList arrText = new ArrayList();
while (objReader.Peek() == 72)
{
sLine = objReader.ReadLine();
if (sLine != "")
arrText.Add(sLine);
}
FileInfo fi = new FileInfo("D:\\Hello.txt");
// Actually create the file.
FileStream fs = fi.Create();
Byte[] fb =
new UTF8Encoding(true).GetBytes(arrText.ToString());
fs.Write(fb, 0, fb.Length);
// Modify the file as required, and then close the file.
fs.Close();
}


这篇关于如何复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆