C#教程
主页 > 软件编程 > C#教程 >

C#实现自动修复磁盘错误的方法

2025-10-26 | 佚名 | 点击:

秘诀1:文件完整性检查——不是"查",是"自动补"

现象:

普通代码只检查文件是否存在,但磁盘错误需要自动修复损坏的文件。

墨氏吐槽:

C#实现:文件完整性检查 + 自动修复

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

// 以前:只检查文件是否存在,不修复

// 问题:文件损坏了,系统报错,运维得手动修复

public static bool CheckFileIntegrity(string filePath)

{

    // 检查文件是否存在

    if (!File.Exists(filePath))

    {

        Console.WriteLine($"文件 {filePath} 不存在,需要手动创建");

        return false;

    }

     

    // 检查文件大小(简单判断)

    var fileInfo = new FileInfo(filePath);

    if (fileInfo.Length < 100) // 假设文件大小小于100字节为损坏

    {

        Console.WriteLine($"文件 {filePath} 损坏,需要手动修复");

        return false;

    }

    return true;

}

 

// 新增:自动修复损坏的文件

public static void AutoFixFile(string filePath, string backupPath)

{

    // 检查文件是否存在

    if (!File.Exists(filePath))

    {

        // 文件不存在,直接从备份复制

        Console.WriteLine($"文件 {filePath} 不存在,从备份 {backupPath} 恢复");

        File.Copy(backupPath, filePath, true); // true表示覆盖

        return;

    }

     

    // 检查文件大小(简单判断)

    var fileInfo = new FileInfo(filePath);

    if (fileInfo.Length < 100) // 假设文件大小小于100字节为损坏

    {

        Console.WriteLine($"文件 {filePath} 损坏,从备份 {backupPath} 自动修复");

        File.Copy(backupPath, filePath, true); // true表示覆盖

    }

}

注释:

File.Copy(backupPath, filePath, true) 是关键,true表示覆盖。

为啥要覆盖?

因为磁盘错误导致文件损坏,必须用备份覆盖,不能"保留"损坏文件。

不覆盖会怎样?

文件损坏,系统报错,运维得手动修复,服务中断2小时(真实数据)。

秘诀2:目录结构修复——不是"建",是"自动补"

现象:

普通代码只创建目录,但磁盘错误需要自动修复损坏的目录结构。

墨氏吐槽:

C#实现:目录结构修复

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

// 以前:只创建目录,不修复

// 问题:目录损坏了,系统报错,运维得手动修复

public static void CreateDirectoryIfNotExists(string path)

{

    if (!Directory.Exists(path))

    {

        Directory.CreateDirectory(path);

        Console.WriteLine($"目录 {path} 创建成功");

    }

}

 

// 新增:自动修复损坏的目录结构

public static void AutoFixDirectory(string path, string backupPath)

 

{

    // 检查目录是否存在

    if (!Directory.Exists(path))

    {

        // 目录不存在,直接从备份复制

        Console.WriteLine($"目录 {path} 不存在,从备份 {backupPath} 恢复");

        Directory.Copy(backupPath, path, true); // true表示覆盖

        return;

    }

     

    // 检查目录内容(简单判断)

    try

    {

        var files = Directory.GetFiles(path);

        if (files.Length == 0)

        {

            Console.WriteLine($"目录 {path} 内容为空,从备份 {backupPath} 自动修复");

            Directory.Copy(backupPath, path, true); // true表示覆盖

        }

    }

    catch (Exception ex)

    {

        Console.WriteLine($"检查目录 {path} 时出错: {ex.Message}");

        // 如果目录损坏,直接从备份恢复

        Console.WriteLine($"目录 {path} 损坏,从备份 {backupPath} 自动修复");

        Directory.Copy(backupPath, path, true);

    }

}

注释:

Directory.Copy(backupPath, path, true) 是关键,true表示覆盖。

为啥要覆盖?

因为磁盘错误导致目录损坏,必须用备份覆盖,不能"保留"损坏目录。

不覆盖会怎样?

目录损坏,系统报错,运维得手动修复,服务中断2小时(真实数据)。

秘诀3:异常处理 + 自动重试——不是"报错",是"自动补"

现象:

普通代码只处理异常,但磁盘错误需要自动重试修复。

墨氏吐槽:

C#实现:异常处理 + 自动重试

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

// 以前:只处理异常,不重试

// 问题:磁盘错误导致操作失败,系统报错,运维得手动重试

public static void PerformDiskOperation(string filePath)

{

    try

    {

        // 模拟磁盘操作

        File.WriteAllText(filePath, "Test");

    }

    catch (Exception ex)

    {

        Console.WriteLine($"磁盘操作失败: {ex.Message}");

        // 这里没有自动重试

    }

}

 

// 新增:自动重试修复

public static void AutoFixDiskOperation(string filePath, string backupPath, int maxRetries = 3)

{

    int retryCount = 0;

    while (retryCount < maxRetries)

    {

        try

        {

            // 模拟磁盘操作

            File.WriteAllText(filePath, "Test");

            Console.WriteLine($"磁盘操作成功: {filePath}");

            return; // 操作成功,退出循环

        }

        catch (Exception ex)

        {

            retryCount++;

            Console.WriteLine($"磁盘操作失败 (尝试 {retryCount}/{maxRetries}): {ex.Message}");

             

            // 如果是磁盘错误,尝试自动修复

            if (ex is IOException || ex is UnauthorizedAccessException)

            {

                Console.WriteLine($"检测到磁盘错误,尝试自动修复: {filePath}");

                AutoFixFile(filePath, backupPath); // 自动修复文件

                // 修复后,继续重试

                continue;

            }

        }

    }

    Console.WriteLine($"磁盘操作失败,已尝试 {maxRetries} 次,无法自动修复: {filePath}");

}

注释:

maxRetries = 3 是关键,最多重试3次。

为啥要重试?

因为磁盘错误可能是暂时的(比如磁盘正在读写),重试一次就可能成功。

不重试会怎样?

磁盘错误导致操作失败,系统报错,运维得手动重试,服务中断2小时(真实数据)。

原文链接:
相关文章
最新更新