0

I have this code in C# that works fine in some users.

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();

    }

 }

}

The code only works if the file is named outlook.ost. How can I change the code in order that rename the OST file search regardless of the name.

Thanks in advance

1 Answers1

0

Iterate through the files in the directory to check if they're .OST and then rename them.

// 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");
    }
}

You should be careful hard coding the .OST path like that. Something like the below would be better:

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

Edit

An better example of replaceOutlook(). Still needs work but better illustrates how this works for 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....
    }
}
Equalsk
  • 7,954
  • 2
  • 41
  • 67
  • yes, thats the main idea. For security policies the OST file is always on the same location but the name is not always the same. I tried that part of your code but I have 23 errors in the rest of the code. Is there a way to change this string ostFile = "Outlook.ost"; to string ostFile = "*.ost"; or something similar? – Marcelo Daniel Fernández Aug 20 '15 at 14:25
  • If you're using the code above correctly you no longer need `ostFile`. In my example it looks at each file in the given directory, check if it's an .OST and if it is it appends .OLD onto the end of the file path. It doesn't look at the filename at all. – Equalsk Aug 20 '15 at 14:29