Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8235009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:39:41+00:00 2026-06-07T18:39:41+00:00

I am using StreamWriter class for file operations, are there any problems in this

  • 0

I am using StreamWriter class for file operations, are there any problems in this code that I am not seeing?

Such as do I need to put it into a try catch finally block?

StreamWriter sr = new StreamWriter(streamFolder);
sr.Write(details);

File.SetAttributes(streamFolder, FileAttributes.Hidden);
sr.Close();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T18:39:43+00:00Added an answer on June 7, 2026 at 6:39 pm

    Whats wrong with your code? If some exception will occur before you close stream, then stream will stay open, and system resources will not be released:

    StreamWriter sr = new StreamWriter(streamFolder);
    sr.Write(details);
    // some exception occurs here 
    File.SetAttributes(streamFolder, FileAttributes.Hidden);
    sr.Close();
    

    So, you need to be sure, that stream will be closed. This could be achieved by try...finally block:

    StreamWriter sr = new StreamWriter(streamFolder);
    
    try 
    {
       sr.Write(details);
       // some exception occurs here 
       File.SetAttributes(streamFolder, FileAttributes.Hidden);
    }
    finally
    {
       sr.Close();
    }
    

    But StreamWriter implements IDisposable interface, so you can let C# compiler do it automatically for you by wrapping writer usage into using block:

    using(StreamWriter sr = new StreamWriter(streamFolder)) 
    {
       sr.Write(details);
       // some exception occurs here 
       File.SetAttributes(streamFolder, FileAttributes.Hidden);
    }
    

    This code will be compiled as:

    StreamWriter sr = new StreamWriter(streamFolder);
    
    try 
    {
       sr.Write(details);
       // some exception occurs here 
       File.SetAttributes(streamFolder, FileAttributes.Hidden);
    }
    finally
    {
       if (sr != null)
          sr.Dispose();
    }
    

    The only difference between manual implementation is null-check and method Dispose is called instead of Close. But actually when you call Close() or Dispose() same code will be executed:

    this.Dispose(true);
    GC.SuppressFinalize(this);
    

    You can read more on Dispose method implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I need to create a file using the StreamWriter class, write to it
I have legacy class that writes some results to a file using StreamWriter ,
I'm using StreamWriter to write log file. And this is the writing log method,
I see this: using (StreamWriter sw = new StreamWriter(file.txt)) { // d0 w0rk s0n
Suppose this C# code: using (MemoryStream stream = new MemoryStream()) { StreamWriter normalWriter =
Is there a way to do this: this.logFile = File.Open(what_r_u_doing.log, FileMode.OpenOrCreate, FileAccess.ReadWrite); using(var sr
I have an example in C# code, but it is using streamWriter . It
I'm using a streamwriter in combination with a background worker, for logging. As such,
If I read and wrote a binary file using StreamReader and StreamWriter, can the
My root problem is that when using calls Dispose on a StreamWriter , it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.