Temporary file Management

C

Codemonkey

Hi,

I have a few questions about best practices when it comes to the management
of temporary files. Any thoughts anyone can give would be much appreciated.

Basically, I'm writing a document storage app - a Windows service acts as
the file sotrage server and remote windows forms clients send files to the
server to be stored. The temporary files become involved when a client
wishes to view a file that is stored on the server (the server sends a byte
array containing the file's data, the client writes this to a temporary
location and then executes the viewing application to view the temporary
file).

My queries are:

1) Who should be responsible for deleting the temporary file?
2) If my client is responsible for deleting the file, how do I know if the
user has closed the viewer application?
3) What is the best way to detect changes to the file if the user edits the
document in the viewer application?

My own thoughts so far are:

1) My client app is responsible for deleting the file (wishful thinking that
it isn't ;)

2) When I launch the process to view the file, I keep track of it and delete
the temp file when the process exits. The problem with this is what do I do
if the user exits my application before they exit the viewer application?
Also, what happens if they keep the viewer open, but opens another file
unrelated to my application?

3) Use FileSystemWatcher to watch the file for changes. Again, what happens
if the user exits my application before exiting the viewer?

Any thoughts would be appreciated,

Best Regards,

Trev.
 
T

Tian Min Huang

Hello Trev,

Thanks for your post. I reviewed your description carefully, and now I'd
like to share the following information with you:
the document in the viewer application?

I believe the best method to work around this problem is to delete the
temporary file in the viewer application. The viewer application need to
delete a temporary file under the following situations:
1. When it will be closes.
2. When a user want to open another file, it needs to detele the previous
one.

Hope this helps. I look forward to your feedback.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
C

Codemonkey

Hi,

Thanks for your help on this, but I have a few extra thoughts:
The viewer application need to
delete a temporary file under
the following situations:
1. When it will be closes.

What happens if my client viewer application is closed before the user had
finished viewing the file (e.g. with Microsoft Word or notepad etc.?) Is
there any way of reliably detecting if the document has been closed and is
safe to delete?
2. When a user want to open
another file, it needs to detele
the previous one.

Again, how can I reliably detect if the user has closed the first file?

Thanks again for your help,

Trev.
 
T

Tian Min Huang

Hello Trev,

Thanks for your reply.
had finished viewing the file (e.g. with Microsoft Word or notepad etc.?)
Is there any way of reliably detecting if the document has been closed and
is safe to delete?

According to my understanding, when the client viewer application is close,
the temp file will at least be closed. I am afraid that we are not able to
know whether a user had finished viewing the file or not when the file is
closed. If you are mentioning the situation when the viewer application is
closed unexpectablly say, power failure, etc. I suggest that you can use
the algorithmn in viewer app below:

1. Viewer app starts.
2. Read the registry for any remaining file information, clear the registry
and delete the files.
3. Log the file information to registry, and then open the file.
4. When the app will be closed nomally, remove the file information from
registry.

Note: You can also log the file manipulation in viewer app to other
persistent storage say, file, instead of the registry.

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
C

Codemonkey

Thanks for the help. When I get around to implementing it, I'll post the
final algorithm here in case anybody else is interested.

Best Regards,

Trev.
 
C

Cor

Hi Codemonkey,

A thoughts of your question that you can use I think.
There is already a thread so I do not repeat, however I did not see this in
it.

There is on every window computer a temp directory (a pity not enumerated by
the special folder).

I think that is the place for your temp files.

A user has tools to delete that or does that by hand.

My 2 eurocents.

Cor
 
C

Codemonkey

Cheers Cor.

Finding the Temp folder is easy enough. Try "System.IO.Path.GetTempPath" or
"System.IO.Path.GetTempFileName" if you want a unique file name.
A user has tools to delete that or does that by hand.

Lol. Yeah. I would have thought by now there would have been a better, more
robust method of managing temproary files built into windows by now. I vote
for a non-persistable virtual RAM disk that can act as a normal folder that
gets cleared on system startup.

Cheers,

Trev.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top