?
Solved

I/O operations.

Posted on 2003-03-24
6
Medium Priority
?
296 Views
Last Modified: 2010-04-15


Hi group, I want to write to a file when an exception ocurs,
I use the FilseStream and StreamWrite classes.

This is the code:
//Creates the FileStream and StreamWrite object
..............
if (File.Exists(fileName))

{

fs = new FileStream(fileName,FileMode.Open,FileAccess.Write);

sw = new StreamWriter(fs);


}

else

{

fs = new FileStream(fileName,FileMode.Create);

sw = new StreamWriter(fs);

.......................

.......................
//The code that writes into a file
try

{

//buffer the string that I want to write

sw.Write(buffer);

}

catch(Exception e)

{

return;

}

It's seems to work but doesn't write anything in the file.
0
Comment
Question by:alarcia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 Comments
 
LVL 4

Accepted Solution

by:
Nebulus_ earned 80 total points
ID: 8193638
I think that you must flush the StreamWriter. This can be done automatically after you create the sw object:

sw.AutoFlush = true;

or manually:

sw.Flush();

after each write op.
0
 

Expert Comment

by:CEILIDH
ID: 8196996
One additional point;

your first if-statement to create a new FileStream is not actually necessary, since the FileMode 'Create' will create a new file if is does not exists, or open the file if it does exist.

You are also not specifiying a FileAccess parameter for your second FileStream, so be sure to check what the Default Access is, since it may be 'Read', and you want to 'Write'.

Good luck.
0
 
LVL 14

Assisted Solution

by:AvonWyss
AvonWyss earned 80 total points
ID: 8202421
Try it like this:

               using (StreamWriter sw=new StreamWriter(new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write), true)) {
                    sw.Write(buffer);
               }

Notes:
- the "using" clause will have the compiler close and dispose the StreamWriter automatically (and therefore also the underlying stream).
- FileMode.Create always deletes an existing file, but OpenOrCreate opens the file if it exists and creates it otherwise.
- The "true" parameter will make sure that data will be appended instead of overwriting existing data in the file.
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 8202428
Even easier...

               using (StreamWriter sw=new StreamWriter(fileName, true)) {
                    sw.Write(buffer);
               }
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question