How to Erase a FileContent in c -Sarp?

  • Thread starter Thread starter Baskar RajaSekharan
  • Start date Start date
B

Baskar RajaSekharan

Hi

Plase let me know how to erase filecontent in C-Sharp with some sample
code.

Regards,
R.Baskar
 
The article says's how to create a file using StreamObject. But my Question
is I want to clear a Content of the File. How to do that? For Example, i
am having one file C:\1.txt, I want to Clear the content of the file.
How to do that in C-Sharp?

Regards,
R.Baskar
 
Hi Baskar!

Please have a closer look at
"[C#] public StreamWriter(string, bool);"

that constructor has the function:
"Initializes a new instance of the StreamWriter class for the specified
file on the specified path, using the default encoding and buffer size.
If the file exists, it can be either overwritten or appended to. If the
file does not exist, this constructor creates a new file."

The second parameter of type bool is named "append":
"Determines whether data is to be appended to the file. If the file
exists and append is false, the file is overwritten. If the file exists
and append is true, the data is appended to the file. Otherwise, a new
file is created."

I am sorry, that I forgot to point you to that constructor in the page.

With kind regards,

Konrad
 
do it thru a concept of DTE rather than streamreaders, it will be very fast. the namespace is ENVDTE.
 
Thanks for the solution.

But my problem is slightly different. I forgot to mention about this. The
file is opened is shared mode. It is being used by some other user.
I have to clear the shared file.

If I use your code it will not allow me to open the file. It is throwing
exception saying file is being used by another process.

This is code I am using.

string sLogFile = @"C:\TimerLog.txt";
FileStream fWriteStream;
if (!File.Exists (sLogFile)) return;
fWriteStream = File.Open(sLogFile,FileMode.OpenOrCreate,
FileAccess.Write ,FileShare.ReadWrite);
StreamWriter sFileWriter = new StreamWriter (fWriteStream);
sFileWriter.Write ("");
sFileWriter.Close ();
fWriteStream.Close ();

This does not clear the file.Pls tell me what is the problem in this code?
 
Hi Baskar!

Baskar RajaSekharan said:
Thanks for the solution.

No Problem!
But my problem is slightly different. I forgot to mention about this. The
file is opened is shared mode. It is being used by some other user.
I have to clear the shared file.

I don't know a solution for this problem. In Windows, you don't have the
right to change it, so you cannot do it with C#. I would say, that it is
impossible, but I am not that Windows-Expert ...
This is code I am using.
string sLogFile = @"C:\TimerLog.txt";
FileStream fWriteStream;
if (!File.Exists (sLogFile)) return;
fWriteStream = File.Open(sLogFile,FileMode.OpenOrCreate,
FileAccess.Write ,FileShare.ReadWrite);
StreamWriter sFileWriter = new StreamWriter (fWriteStream);
sFileWriter.Write ("");
sFileWriter.Close ();
fWriteStream.Close ();
This does not clear the file.Pls tell me what is the problem in this
code?

Hmm ... my idea was using the constructor that way:
StreamWriter sFileWriter = new StreamWriter ("C:\TimerLog.txt", false);
but if the file is in use ... I think, that you cannot do this with
windows, but mayne there is another expert, that can correct me!

With kind regards,

Konrad
 
I agree that if someone else has a lock on the file, you will not be able to
change it in any way.

If you need to have multiple people making changes to the file at the same
time, you might consider creating a service of some sort that each person
has to go through to make changes to the file (for example, write a remoted
object that provides methods to append text to the file, and to clear it).
This is a lot more work than just writing to the file, but it would solve
your problem.

Eric
 
Back
Top