在C#中重命名OST文件

时间:2015-08-20 14:05:46

标签: c#

我在C#中使用此代码,在某些用户中运行良好。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace Rename_OST
{
class Program
{
    static public void killOutlook()
    {
        try
        {
            string process = "OUTLOOK";
            foreach (Process outLook in Process.GetProcessesByName(process))
            {
                outLook.Kill();
            }
        }
        catch (Exception) { }
    }
    static public void startOutlook()
    {
        try
        {
            //busca el path del Outlook
            Process.Start("OUTLOOK");
        }

        catch (Exception)
        {
            Console.WriteLine("Could'n open Outlook. Please start Outlook and press any key.");
            Console.ReadKey();
        }
    }
    static public void replaceOutlook()
    {
        string ostPath = "C:\\Users\\" + Environment.UserName + "\\AppData\\Local\\Microsoft\\Outlook\\";
        string ostFile = "Outlook.ost";
        string ostNewFile = "Outlook.ost.txt";
        try
        {
            if (!File.Exists(ostPath + ostNewFile))
            {
                File.Move(ostPath + ostFile, ostPath + ostNewFile);
            }
            else
            {
                File.Delete(ostPath + ostNewFile);
                File.Move(ostPath + ostFile, ostPath + ostNewFile);
            }
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine("The OST file was not found.");
            Console.ReadKey();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.ReadKey();
        }
    }
    static void Main(string[] args)
    {
        Console.WriteLine("Closing Outlook client...");
        killOutlook();
        Console.WriteLine("Replacing OST file name...");
        Thread.Sleep(5000);
        replaceOutlook();
        Thread.Sleep(5000);
        Console.WriteLine("Starting Outlook client...");
        startOutlook();

    }

 }

}

仅当文件名为outlook.ost时,代码才有效。如何重命名代码以重命名OST文件搜索而不管名称如何。

提前致谢

1 个答案:

答案 0 :(得分:0)

遍历目录中的文件以检查它们是否是.OST然后重命名它们。

// GET ALL FILES IN DIRECTORY
string[] fileEntries = Directory.GetFiles(ostPath);

// CHECK EACH FILE
foreach (string fileName in fileEntries)
{
    // IS IT AN OST?
    if (Path.GetExtension(fileName).ToLower() == ".ost")
    {
        // RENAME LOGIC HERE, EXAMPLE:
        File.Move(fileName, fileName + ".OLD");
    }
}

你应该小心对.OST路径进行硬编码。像下面这样的东西会更好:

string ostPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Microsoft\Outlook");

修改

replaceOutlook()的一个更好的例子。仍然需要工作,但更好地说明了它如何适用于OP。

static public void replaceOutlook()
{
    // OST PATH
    string ostPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"Microsoft\Outlook");

    // LIST OF FILE PATHS IN OST PATH
    string[] fileEntries = Directory.GetFiles(ostPath);

    try
    {
        // CHECK EACH FILE PATH
        foreach (string fileName in fileEntries)
        {
            // IS IT AN OST?
            if (Path.GetExtension(fileName).ToLower() == ".ost")
            {
                // TRY AND DELETE OLD FILE, WON'T THROW EXCEPTION IF FILE DOESN'T EXIST
                File.Delete(fileName + ".OLD");

                // RENAME FILE
                File.Move(fileName, fileName + ".OLD");
            }
        }
    }
    catch
    {
        // Blah blah....
    }
}