0

I need some help.

My application works with Outlook files. For example, I have a MSG format file is stored at my PC. Let's imagine that I haven't got any running Outlook processes on my PC.

var myProcess = new Process { ProcessInfo = new ProcessStartInfo {FileName = targetPath}}

where targetPath is full path to my file. Then I start process:

myProcess.Start();

At this moment Outlook runs at OS. Looking into TaskManager I see this process, and it's unique PID. And there is my first question: why this PID is different from myProcess.Id ?

Moving on. I need to check is my file still opening at Outlook. I resolve this issue by trying to open this file in my application for a certain time.

var iterations = 3600;
while (iterations > 0 ) {
    Thread.Sleep(1000);
    var fInfo = new FileInfo(targetPath);
    FileStream stream = null;
    try {
        stream = fInfo.Open(FileMode.Open, FileAccess.Read | FileAccess.Write, FileShare.None);
    } catch {
        //I expect if exception occurs then file is not lock and can send it to server.
    }
    finally{
        if(stream != null)
        stream.Close();
    }
iteration--;
}

I think, while my MSG file is using by Outlook, I my application cant open file. In that way I decide that cant save my file and send it to server. But! If I add attachment but NOT close this e-mail at Outlook, my application CAN open this file. And I don't understand why Outlook change Read/Write attribute of this file? And how I can solve this issue?

Unfortunately, I haven't any idea why it happens and how to make it work.

I was looking on any info at web that can help to solve my issue, but has no result :(

Thank you for your time.

vpopov
  • 1
  • 1
  • [Please don't put tags in question titles](https://stackoverflow.com/help/tagging) – Liam Jun 15 '17 at 08:57
  • I'm not following what this issue is? Can you elaborate? FYI always `Dispose()` of `FileStream`(s) – Liam Jun 15 '17 at 09:00
  • As suggested here, if you are getting incorrect process id from myProcess.ID, you can try using handles to get the id: [getting PID of process started by Process.start()](https://stackoverflow.com/a/12892316/6741868) – Keyur PATEL Jun 15 '17 at 09:14
  • As @Liam mentions, your second question is not clear, what do you mean by this "at this moment file becomes available for open"? – Keyur PATEL Jun 15 '17 at 09:16
  • Thank you for your comment. Of cause, I can. I start while cycle, put into opening file code, and repeat last operation for a certain time. While my MSG file is using by Outlook, I my application cant open file. In that way I decide that cant save my file and send it to server. But! If I add attachment but NOT close this e-mail at Outlook, my application CAN open this file. And I don't understand why Outlook change Read/Write attribute of this file? And how I can solve this issue? – vpopov Jun 15 '17 at 09:28
  • @Keyur PATEL is it clear now? – vpopov Jun 15 '17 at 09:39
  • @Liam please, look at my answer. – vpopov Jun 15 '17 at 09:40
  • Please avoid adding details in comments, edit the question – Liam Jun 15 '17 at 09:50

1 Answers1

0

Firstly, Outlook is a singleton - if you start a new instance of outlook.exe, it will simply switch control to the already running instance and exit.

Outlook internally caches / references open MSG files, there is really northing you can do about that.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78