简体   繁体   中英

Only Open file with process.start if not open yet

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM