简体   繁体   English

如果尚未打开,则仅使用 process.start 打开文件

[英]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. Process.Start 似乎是最好的选择,但我愿意接受建议。

This question seems similar but I can't get it to work: Is there a way to check if a file is in use?这个问题看起来很相似,但我无法让它工作: 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.在约会屏幕中,他们可以双击 GridView 中的文件将其打开。 These files can be anything from pdf's to msg (Mails).这些文件可以是从 pdf 到 msg(邮件)的任何文件。

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.嗯,听起来像是您想以列表的形式保留所有当前打开的文件路径,以便将下一个文件路径与其进行比较,如果文件已经打开则拒绝尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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