使用.bak扩展名备份现有文件并将原始文件保留在C#中 [英] Backing up the existing file with .bak extension and keeping the original one in C#

查看:102
本文介绍了使用.bak扩展名备份现有文件并将原始文件保留在C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用.bak扩展名备份现有文件,我应该将原始文件保存在同一目录中C#



任何人都可以帮助我。



提前致谢。

I Want to Backing up the existing file with .bak extension and i should keep the original one in same directory C#

Could any one help me on this.

Thanks in advance.

推荐答案





如果您想在文件夹中保留相同的副本,那么您可以使用此代码

Hi,

If you want to retain the same copy in both the folder then you can use this code
using System.IO;
File.Copy(@"c:\test.txt",@"c:\backup\test.bak"); //Parameter1: Source file path, Parameter2: destination file path.



如果你想维护一个副本备份那么你可以使用移动而不是复制

两者都类似于重命名文件。

希望这对你有所帮助。



问候,

RK


And if you want to maintain a single copy to back up then you can use Move instead of Copy.
Both are similar to renaming the file.
Hope this helps you a bit.

Regards,
RK


基本上解决方案1解释了这一切,但在我提供的链接上有一个示例代码:

C#Multiple File.Copy()with backup [< a href =http://www.dreamincode.net/forums/topic/290647-c%23-multiple-filecopy-with-backup/\"target =_ blanktitle =New Window> ^ ]



祝你好运,

OI
Basically Solution 1 explains it all but here on the link I provided you have a sample code:
C# Multiple File.Copy() with backup[^]

Good luck,
OI


一般来说,你的备份计划将包括这些步骤(假设您已经解决了与fil相关的所有问题e写权限):



0.在项目中为System.IO设置一个引用:
In general your backup plan will include steps like these (assuming you've already solved all issues related to file write permission):

0. set a reference in your project to System.IO:
using System.IO;

1。指定您的路径名:

1. specify your path names:

private string backupDirectory;
private string filePath;
private string backUpPath;

// we'll use a simple string to test saving, and backup:
private string someText = @"Test save a file.";

1。初始化路径变量(在某些EventHandler中,如Form Load:

1. initialize your path variables (in some EventHandler like Form Load:

backupDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
filePath = backupDirectory + @"\testFile.txt";
backUpPath = backupDirectory + @"\testFile.bak";

定义一个方法,您可以按需或按计划调用,执行保存和backup:

Define a method that you can call on-demand, or when scheduled, that performs the save and backup:

private void BackUpAndSave()
{
    // does the file exist ?
    if (File.Exists(filePath))
    {
        // if the backup exists, delete it
        if(File.Exists(backUpPath)) File.Delete(backUpPath);
        
        // backup the current saved file
        File.Copy(filePath, backUpPath);

        // delete the current saved file
        File.Delete(filePath);
    }

    // save the file
    using (StreamWriter sWriter = new StreamWriter(filePath))
    {
        sWriter.Write(someText);
    }
}

您会注意到此代码会在第二次保存文件时创建备份,并且创建的备份版本将滞后一步保存文件后面:如果这不是您想要的,您可以更改代码,以便备份始终与当前保存的文件版本保持同步。我使用这种类型的代码来实现撤销/重做功能。

You'll note this code creates a back-up the second time the file is saved, and that the back-up version created will "lag" one step behind the file saved: if that's not what you want, you can change the code so the back-up is always in synch with the current saved version of the file. I use this type of code for undo/redo functionality.


这篇关于使用.bak扩展名备份现有文件并将原始文件保留在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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