[英]How do I check if a file is already open?
private void btnViewErrorLogFile_Click(object sender, EventArgs e)
{
errorLogFileProcess = Process.Start(AppVars.ErrorLogFilePath);
}
上面在記事本中打開一個文本文件。 它包含我的應用程序的錯誤日志。
我不想允許用戶打開該文件的倍數。 因此,我不想讓他/她再次單擊該按鈕並打開同一文件的另一個窗口。
如何檢查此文件是否打開? 注意:我不想關閉(或全部)notepad.exe進程,因為用戶可能會為我的應用程序文件(打開文件時使用notepad.exe)以外的其他東西打開記事本進程。
因此,我又該如何檢查打開的進程是否已經打開?
剛剛再次閱讀了您的問題。
如果要強制用戶關閉(啟動的)記事本的活動實例,然后再允許其打開,請保留errorLogFileProcess
實例errorLogFileProcess
,然后在允許該命令之前對其進行重新檢查。
Process errorLogFileProcess;
private void btnViewErrorLogFile_Click(object sender, EventArgs e)
{
if (errorLogFileProcess != null)
{
// Process was launched previously; check if exited.
if (!errorLogFileProcess.HasExited)
{
throw new Exception("Can't launch until first one closed.");
}
}
errorLogFileProcess = Process.Start(AppVars.ErrorLogFilePath);
}
也可以使用errorLogFileProcess.HasExited
狀態禁用按鈕,以便不會引發Click事件。
在此處檢查如何檢查文件是否已打開(通過使用try,catch塊): 檢查文件是否已打開
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.