Click here to Skip to main content
15,906,625 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
foreach (string file in dirs)
{
   using(FileStream flStream = new FileStream(file, FileMode.Open, FileAccess.Write))
  {
      MessageBox.Show(file);
  }	
}


After msgbox.show the Stream closes. How to keep each file opened? Just, hard to imagine how..
thanks.
Posted
Updated 20-Apr-10 10:58am
v2

Remove the using block and make sure you dispose of flStream yourself when you're done with it.
 
Share this answer
 
Why would you want to? If your finished with it then close the file and stop using the resources.

Because the FIleStream is wrapped in a using block of course it will close since after leaving the using scope Dispose will be called on the FileStream object which will in turn call close. If you want to keep it open, then don't use using. However, keep in mind you need to implement some mechanism to destroy resources when you are finished with them.
 
Share this answer
 
First, why would you want to keep each file open?

Second, you're only showing the file name...that doesn't need to be in the using statement

Third, the file closes because you exit the using statement. Put anything you want to do with the FileStream inside the using statement.
 
Share this answer
 
Is there any specific reason why you'd want that?

I guess you could make an array like this:

C#
FileStream[] fileStreams = new FileStream[dirs.Length];

for (int i = 0; i < dirs.Length; i++)
{
    fileStreams[i] = new FileStream(dirs[i], FileMode.Open, FileAccess.Write);
}


Again I do not encourage this at all! If you do this just make sure to do another for loop once you're done to close all the FileStreams and dispose them!
 
Share this answer
 
v2

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