Click here to Skip to main content
15,887,442 members
Please Sign up or sign in to vote.
4.67/5 (3 votes)
See more:
How can i write a single method for

if a given file is opened then close and vice versa?
Posted
Comments
Sergey Alexandrovich Kryukov 25-May-11 10:52am    
Makes no sense as formulated. Would you clarify on code/pseudocode sample?
--SA

use using block to make sure each n every time file is closed when you open it.... like...

C#
using (StreamWriter sw = new StreamWriter(fullFilePath, true))
{
     sw.WriteLine(string.Format("String that you want to append"));
}

you can also lock the other object with the use of lock keyword.....
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-May-11 10:54am    
This is ***certainly*** not OP means. I suspect what OP wants makes no sense though.
However, this is the technique to be used in adequate coding style, therefore my 5.
--SA
parmar_punit 26-May-11 0:32am    
As per my knowledge there is not any easy way to know whether the file is opened or closed, but this may be the solution to avoid checking for opened or closed file...
thats why i directly go to the point. May this code help to RAJI@Codeproject...
and you haven't give me vote yet as you write "therefore my 5"... :)
Sergey Alexandrovich Kryukov 28-May-11 1:52am    
I decide to vote but later forgot sorry. Please don't hesitate to remind -- such things often happened, not only with me.

Please look at my answer.
I understand your motivation and agree. I still think the whole idea of acquisition of the file status at arbitrary moment of time would be a serious abuse. If you analyze the principles of the control of the file handle lifetime I suggest you will probably agree this is the only safe and robust discipline.
--SA
Sergey Alexandrovich Kryukov 25-May-11 11:04am    
I formulated the design principle for using file/stream handles/references which should never be violated. What OP wants would be an abuse.

That's why the pattern you've should here can always be applied and what OP wants should never be.

Please see my solution.
--SA
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 25-May-11 11:05am    
I think the whole idea would be a grave technology abuse.
Please see my solution and my comments to the solution by Parmar.
--SA
I still don't know the details, but I'm sure the whole idea of working with the file object with unknown status it a huge abuse of the technology.

The right idea would be: a file/stream variable should never be visible outside the stack starting from the point the file is first open. It should never be a static variable or even a class field. You need to open and close the file in one single method. You should use the pattern shown in the answer by Parmar.

You can actually pass the file/stream variable to other methods but only as a parameter (because write/read operations on the same stream could be quite complex and need some abstraction tools), in this way the principles I formulated in the previous paragraphs would not be violated.

If you want to violate these principles, get ready to some trouble. Better don't do it!

—SA
 
Share this answer
 
v2
Why? It is fairly easy to achieve but why? It does not makes sense to have a method like that. Whosoever uses your method will never know whether file will be opened or closed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900