0

in my program people can open any sort of file with the method below. I got the feedback that they can open the same file more than once. Meaning having the same file open a few times at the same time. How do I prevent this? Process.Start seemed the best option but I am open to suggestions.

This question seems similar but I can't get it to work: Is there a way to check if a file is in use?

Used method:

public void ShowFile(ModelDocument model)
    {
        if (model != null)
        {
            try
            {
                string locationFileName = model.DocumentPath;
                using (Process process = new Process())
                {
                    ProcessStartInfo startInfo = new ProcessStartInfo();
                    process.StartInfo = startInfo;
                    startInfo.FileName = locationFileName;
                    process.Start();
                }
            }
            catch (System.Exception ex)
            {
                //log error
            }
        }
    }

More context:

I have a scheduler where people can attach any document/mail to an appointment.

These get copied or moved to a folder specific for that appointment. In my database I store the path and other info concerning the file. In the appointment screen they can double-click a file in a GridView to open it. These files can be anything from pdf's to msg (Mails).

1 Answers1

0

Hm, sounds like a case where you want to keep all currently opened files paths at hand in a form of a list to compare the next file path against it and decline attempt if the file is open already.

Xander
  • 378
  • 1
  • 6
  • yes, it's the question. but your is not answer. – Lei Yang Feb 17 '22 at 09:11
  • I thought of that but how do I update that list if the file gets closed? And they want to reopen it... – Jurgen Volders Feb 17 '22 at 09:44
  • @JurgenVolders that depends on your architecture, but ideally you should have control over what is happening with the file. Just delete the reference from dictionary if the file was closed and its content was disposed form memory. – Xander Mar 04 '22 at 11:01