0

I am creating a file (e.g. myfile.sql) and I want to open the file in the associated application (e.g. SSMS):

Process proc = new Process();
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.FileName = FileName;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
proc.Start();

After the process has started I want to evaluate its id. The behavior depends on the state before proc.Start(). When the application has not been already running the .Id property contains the id of created process. When the application has been already running the operating system doesn't open new instance but brings the application to the front and application asks whether to reload the file and so on. The problem is that when I consult .Id at this case I am getting an InvalidOperationException. WaitForInputIdle or Handle behave the same way. Refresh doesn't improve the situation.

Is it possible to get the id of the process, which was already running without looking for a window with appropriate title (c# Get process window titles)?

As a temporary (?) solution I use:

try
{
    return proc.Id;
}
catch
{
    return ProcessHelper.GetActiveProcessId();
}

The ProcessHelper is from How do I check if a certain process has focus?

Note: The file is not locked (no reference is held) by the process and even the Process Explorer is not able to find a handle. I is possible to rename or delete the file while is open without any OS protest.

IvanH
  • 5,039
  • 14
  • 60
  • 81
  • You may have a look at [`handle` utility](https://learn.microsoft.com/en-us/sysinternals/downloads/handle) – Pavel Anikhouski Jan 29 '21 at 11:29
  • @PavelAnikhouski: the handle utility does not finds the file. Only when it is locked (e. g. opened in MS-Word) it is detected. – IvanH Jan 29 '21 at 11:47
  • Hang on, this question isn't about a file **at all**. Might want to cut down the question and remove all references to that unrelated detail. Instead, you appear to want to create a single-instance application, that optionally communicates with an already running instance. A question that has been asked and answered multiple times. Do some research. – IInspectable Jan 30 '21 at 08:05
  • @IInspectable I do not need to communicate with opened application. The opened application is not main (supposed to be [SSMS](https://learn.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms?view=sql-server-ver15)). I just need to know its id (I want to check whether it still has focus). – IvanH Feb 01 '21 at 09:17
  • I'm sure you're just throwing about terms you do not understand. You're going to have to *identify* the problem you're trying to solve prior to asking a question. This question is neither answerable, nor would it address the problem you are trying to solve. Quite possibly, this is just another variation of the [XY Problem](https://xyproblem.info). – IInspectable Feb 01 '21 at 09:20
  • You are partially right with XY problem because I can use (with decreased reliability) a process having the focus. However by my opinion the problem is simple. I run the code from question, which I consider to be equivalent of double clicking the file in file explorer. I want to know which process takes care of my file. The problem occurs when the process is already running. I should have also been more precise with lock/reference terms, but I am not sure which one is correct. – IvanH Feb 01 '21 at 16:02

0 Answers0