简体   繁体   中英

How do I close an open file?

This button opens a text file which has error log information:

private void btnViewErrorLogFile_Click(object sender, EventArgs e)
{
    Process.Start(AppVars.ErrorLogFilePath);
}

When the user goes to do any processing in the application, I want to check whether the file is open or not, if it is open, then I want to close it. How would I go about doing this?

This example is almost identical to what you're trying to do: Process.Close Method

Process myProcess;

private void btnViewErrorLogFile_Click(object sender, EventArgs e)
{
    myProcess.Start(AppVars.ErrorLogFilePath);
}

private void doSomething()
{
    if (!myProcess.HasExited)
    {
      myProcess.CloseMainWindow();
      myProcess.Close();
    }

    // Do whatever you need with the file
}

Shows how to check if it's running and how to close it.

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