本文介绍了复制,创建和更新文件,保持原始文件在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的文件(文件名)复制到文件夹备份电话。当文件已被成功复制到备份文件夹中,modifySQLFile()将比读取备份文件夹中的文件,并更新其上的文件,并保留原始文件(文件名),它平常的地方。当程序运行的第二次,它会读取原始文件并创建另一个副本备份和更新复制的文件并覆盖previous复制的文件。

不过,我不知道我的codeS错在哪里,怎么办现有文件的覆盖。请,帮助我,因为我是新来这个。

 公共字符串文件名=DEPOT-Pub_Sub_Combined(WO持卡人)的.sql私人无效modifySQLFile()
{
    CopyFile();    字符串[] = fileTexts File.ReadAllLines(@备份\\ DEPOT-Pub_Sub_Combined(WO持卡人)的.sql);
    INT计数器= 0;    //文件处理
    的foreach(在fileTexts串线)
    {
        //只处理非行的评论
        如果(line.StartsWith( - )==假)
        {
            //代替服务器名称的实例
            如果(line.Contains(服务器)==真)
            {
                fileTexts [计数器] = fileTexts [窗口] .Replace(服务器,textBox1.Text);
            }            如果(line.Contains(ACCESSID)==真)
            {
                fileTexts [计数器] = fileTexts [窗口] .Replace(ACCESSID,textBox2.Text);
            }            如果(line.Contains(NETWORKID)==真)
            {
                fileTexts [计数器] = fileTexts [窗口] .Replace(NETWORKID,textBox2.Text);
            }
        }
        反++;
    }
    //更新文件
    File.WriteAllLines(文件名,fileTexts);    的MessageBox.show(完成!);
}私人无效CopyFile()
{    字符串TARGETPATH​​ = @备份
    字符串destFile = Path.Combine(TARGETPATH​​,文件名);    如果(!Directory.Exists(TARGETPATH​​))
    {
        Directory.CreateDirectory(TARGETPATH​​);
    }   File.Copy(文件名,destFile,真正的);
}


解决方案

首先回答你的问题。你正在写在错误的文件modifySQLFile()方法。应 destFileName 而不是文件名

File.WriteAllLines(文件名,fileTexts);

其次,假设该文件不是很大(小于10MB),更好,更简单的方法将是读你的原始文件的内容到内存中,修改的内容,然后将其写入备份。没有必要为 CopyFile()

I am trying to copy my file (fileName) into a folder call backup. When the file have been successfully copied into the backup folder, the modifySQLFile() will than read the file from backup folder and update the file in it and keep the original file (fileName) at it usual place. When the program run for the 2nd time, it will read the original file and create another copy to backup and updated the copied file and overwrite the previous copied file.

However, i am not sure what my codes went wrong and how to do the overwriting of the existing file. Kindly, help me out as i'm new to this.

public string fileName = "DEPOT-Pub_Sub_Combined (wo CardHolder).sql";

private void modifySQLFile()
{
    CopyFile();

    string[] fileTexts = File.ReadAllLines(@"backup\DEPOT-Pub_Sub_Combined (wo CardHolder).sql");


    int counter = 0;

    //File processing 
    foreach (string line in fileTexts)
    {
        //only process non-comments line
        if (line.StartsWith("--") == false)
        {
            //replace instances of server name
            if (line.Contains(SERVERNAME) == true)
            {
                fileTexts[counter] = fileTexts[counter].Replace(SERVERNAME, textBox1.Text);            
            }

            if (line.Contains(ACCESSID) == true)
            {
                fileTexts[counter] = fileTexts[counter].Replace(ACCESSID, textBox2.Text);
            }

            if(line.Contains(NETWORKID) == true)
            {
                fileTexts[counter] = fileTexts[counter].Replace(NETWORKID, textBox2.Text);
            }


        }
        counter++;
    }
    //update file
    File.WriteAllLines(fileName, fileTexts);

    MessageBox.Show("Completed!");
}

private void CopyFile()
{

    string targetPath = @"backup";
    string destFile = Path.Combine(targetPath, fileName);

    if(!Directory.Exists(targetPath))
    {
        Directory.CreateDirectory(targetPath);
    }

   File.Copy(fileName, destFile, true);
}
解决方案

First to answer your problem. You are writing to the wrong file in the modifySQLFile() method. Should be destFileName instead of fileName

File.WriteAllLines(fileName, fileTexts);

Secondly, Assuming this file is not huge (<10MB), the better and easier way will be to read the contents of your original file into memory, modify the contents and then write it into backup. There is no need for CopyFile()

这篇关于复制,创建和更新文件,保持原始文件在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 06:07