JJ.
JJ.

Reputation: 9950

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?

Upvotes: 1

Views: 153

Answers (1)

crlanglois
crlanglois

Reputation: 3609

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.

Upvotes: 1

Related Questions