Avatar of Tom Knowlton
Tom Knowlton
Flag for United States of America asked on

Close file if open

I have code that opens a file.

This is what I am using:

Process.Start("C:\\Program Files\\Adobe\\Acrobat 8.0\\Acrobat\\Acrobat.exe", SavePath);

Is there a way to detect if the file is open already and close it if it is?  Even if another program has that file open?
C#

Avatar of undefined
Last Comment
silemone

8/22/2022 - Mon
silemone

are you trying to close that process or that file?  Can you show a little more code, please.
What exactly is Process?  Anyway, I have example code for opening closing file


FileInfo objFI = new FileInfo(@"c:\myFile2.txt");
FileStream objFS = objFI.Open(FileMode.Create, FileAccess.ReadWrite);
StreamWriter objSW = new StreamWriter(objFS);
objSW.Write(strResult.ToString());
objSW.Write(strResult2.ToString());
objSW.Close();
objFS.Close();
objSW.Dispose();
objFS.Dispose();
Tom Knowlton

ASKER
I am not sure how to distinguish between process and file.

string SavePath = "C:\\GamePlanReport_for_ID_" + def.CandidateID.ToString() + ".pdf";

def.DocumentReport.Publish(SavePath, FileFormat.PDF);

Process.Start("C:\\Program Files\\Adobe\\Acrobat 8.0\\Acrobat\\Acrobat.exe", SavePath);


I am running my code in Debug mode, which saves the PDF out to the hard drive, then proceeds to open it.

Sometimes I forget to close the file before I repeat the above process (run the debugger again).  So I get an exception that the file is in use by another process (which makes sense).

I want to programmatically close the file, then proceed with stepping through my code.
silemone

This is me just posing questions, but:

And there's No Process.Stop or Process.Close, Process.End?

Can you go  Process.Dispose() or Process = null?  and if  you did, would that leave a file open on your screen?  

use a try...catch to handles that  exception. and maybe you can  Use FileStream to Close the File.  
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
silemone

can you give me the API for Process...Is that a static class?
silemone

Oh..i get it...you're actually starting a real process...uhm...
silemone

ok...i understand...to close the process you're calling Process.Kill();
That's not closing the file when you end the process?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Tom Knowlton

ASKER
I never call Process Kill.
silemone

?  
you want the process to run forever?
ASKER CERTIFIED SOLUTION
silemone

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.