title: How detect process open with no mistakes?
body: <p>I'm conducting a routine that allows user to open files and encode them as soon they're closed.</p>
  
  <p>To detect when the file is open AND when the file is already closed, I monitor processes search for its name (received by parameter Argument_Name):</p>
  
  <pre><code>    Dim processes As Process() = Process.GetProcesses()
  
      Try
          For Each p As Process In processes
              Dim windows As IDictionary(Of IntPtr, String) = GetOpenWindowsFromPID(p.Id)
              For Each kvp As KeyValuePair(Of IntPtr, String) In windows
                  If kvp.ToString.Contains(Argument_Name) Then
                      return true
                  End If
              Next
          Next
      End Try
  
      return false
  </code></pre>
  
  <p>I check the name with and without extension, since some applications seems to report it in Process-List differently - some puts the entire name and extension while other puts only the name.</p>
  
  <p><strong>My problem:</strong></p>
  
  <p>1- My user opened an Office file called "x.docx"</p>
  
  <p>2- It seems Word had saved only the main name "x"- maybe because the document TITLE is just "x" (the title you can define within document properties when "save as")</p>
  
  <p>3- So, because of Office, in the above code I've to check just names withou extensions and/or path (<em>kvp.ToString.Contains()</em>).</p>
  
  <p>Using this logic, I get WRONG results because ANY process with a "x" letter in name will be pointed as the true "x.docx".</p>
  
  <p><strong>QUESTION:</strong></p>
  
  <ul>
  <li>How can I detect without any mistake the opened process from my application?</li>
  <li>How can I detect if a process is related to a filename without that colateral effect?</li>
  </ul>
  
  <p>Do you have any idea to make it better?</p>
  
  <p>I appreciate any help on this.</p>