Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
private static void CloseOutlook()
{
    if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
     {
         var app = Marshal.GetActiveObject("Outlook.Application") as Outlook._Application;
         app.Quit();
     }
}
public class FileMove
{
    static void Main()
    {
        string sourceFile = @"C:\Users\IBM_ADMIN\AppData\Local\Microsoft\Outlook\rahul.dinesh@bp.com.ost";
        string destinationFile = @"C:\Users\IBM_ADMIN\AppData\Local\Microsoft\rahul.dinesh@bp.com.ost ";
        System.IO.File.Move(sourceFile, destinationFile);
    }
}   
Outlook error: "Outlook has stopped working"   1. Close Outlook and Skype 2. Move the ost file (C:\Users\uxxxxxxx\AppData\Local\Microsoft\Outlook) to upper folder) 3. Restart Outlook.   



private static void RestartOutlook()
{
    if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
     {
         var app = Marshal.GetActiveObject("Outlook.Application") as Outlook._Application;
         app.Quit();
     }
 
     Thread.Sleep(500);
     Process process = new Process();
     process.StartInfo = new ProcessStartInfo(OutlookFilepath);
     process.Start();
}


What I have tried:

Please help in trouble shooting the C# code
Posted
Updated 29-Oct-18 7:50am
v2
Comments
Patrice T 28-Oct-18 17:13pm    
Describe "not working".
what is it supposed to do?
Member 14035806 28-Oct-18 20:17pm    
It is supposed to close Outlook, move the .ost file to another folder and then restart Outlook.

These steps are for automating an error while trying to launch Outlook.
#realJSOP 28-Oct-18 17:23pm    
The reason it isn't working is because your code isn't in a <pre> block.
Member 14035806 28-Oct-18 20:17pm    
Could you please help me with an resolution or suggestions for correcting the code.
#realJSOP 29-Oct-18 13:33pm    
Learn to use the debugger. "This code isn't working" is NOT a question.

1 solution

Well, I'm going to try divining the solution with my crystal ball because of yourvaguely-worded non-question.

Your method to close Outlook should look more like this:

C#
private static void CloseOutlook()
{
    // get all of the instances of outlook that are currently running
    Process[] outlooks = Process.GetProcessesByName("OUTLOOK");

    // kill each one, one at a time
    foreach (Process outlook in outlooks)
    {

        // kill the process
        int id = outlook.Id;
        outlook.Kill();

        // loop until the process is dead
        Process proc = null;
        do
        {
            proc = Process.GetProcessById(id);
            if (proc != null)
            {
                Thread.Sleep(250);
            }
        } while (proc <> null);
    }
}


To restart outlook, you need to find the executable on the system, and execute it. Look for the appropriate registry entry to find the install location, and use the Process class to start it.
 
Share this answer
 
Comments
[no name] 29-Oct-18 14:22pm    
He has no code calls in his "Main" method (except "move"); the other methods are orphans ... it's all very sad.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900