小编swa*_*nil的帖子

在C#中将数据附加到现有文件

我编写了以下代码来附加现有数据的数据,但我的代码覆盖了这一点

我应该怎么做代码附加数据的更改.

protected void Page_Load(object sender, EventArgs e)
{
    fname = Request.Form["Text1"];
    lname = Request.Form["Text2"];
    ph = Request.Form["Text3"];
    Empcode = Request.Form["Text4"];

    string filePath = @"E:Employee.txt";
    if (File.Exists(filePath))
    {
        //StreamWriter SW;
        //SW = File.CreateText(filePath);
        //SW.Write(text);
        //SW.Close();
        FileStream aFile = new FileStream(filePath, FileMode.Create, FileAccess.Write);
        StreamWriter sw = new StreamWriter(aFile);
        sw.WriteLine(Empcode);
        sw.WriteLine(fname);
        sw.WriteLine(lname);
        sw.WriteLine(ph);
        sw.WriteLine("**********************************************************************");

        sw.Close();
        aFile.Close();
    }
    else
    {
        //sw.Write(text);
        //sw.Flush();
        //sw.Close();
        //StreamWriter SW;
        //SW = File.AppendText(filePath);
        //SW.WriteLine(text);
        //SW.Close();

        FileStream aFile = new FileStream(filePath, FileMode.Append, FileAccess.Write);
        StreamWriter sw = new StreamWriter(aFile);

        sw.WriteLine(Empcode); …
Run Code Online (Sandbox Code Playgroud)

c#

7
推荐指数
2
解决办法
4万
查看次数

从c#中的文件夹中复制所有类型格式文件

我试图将所有格式文件(.txt,.pdf,.doc ...)文件从源文件夹复制到目标.

我只为文本文件编写代码.

我该怎么做才能复制所有格式文件?

我的代码:

string fileName = "test.txt";
string sourcePath = @"E:\test222";
string targetPath =  @"E:\TestFolder"; 

string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
Run Code Online (Sandbox Code Playgroud)

复制文件的代码:

System.IO.File.Copy(sourceFile, destFile, true);
Run Code Online (Sandbox Code Playgroud)

c#

4
推荐指数
2
解决办法
7321
查看次数

标签 统计

c# ×2