Situ


I am opening afile through

foreach(string eachfile in ArrayOfFile)
{

System.IO.StreamReader sr = new System.IO.StreamReader(eachfile,FileMode.Open,FileAccess.Read);
System.Text.StringBuilder strAllText = new System.Text.StringBuilder();

}


I want that if any one file is not access or not open then it will throw a error msg only and continue to read another file..

if anyone has solution then plz reply



Re: .NET Base Class Library How to check file is open or not

ahmedilyas

simply using a try catch block to catch the exception and move onto the next file.

foreach(string currentFile in ArrayOfFile)

{

try

{

//open file and do whatever

}

catch (UnauthorizedAccessException)

{

//they dont have permission to read/access the file

}

catch (IOException ex)

{

//there was an I/O Error, which also covers accessibility of files

}

}

this will keep looping through the files even if an error has occured as the try {} catch {} blocks are implemented within the foreach loop.






Re: .NET Base Class Library How to check file is open or not

Situ

Thanks for reply..

I did this already

foreach (string Findstr in arr)

{

String Filepath = Findstr;

System.IO.StreamReader sr = new System.IO.StreamReader(Filepath);//FileMode.Open.ToString(), FileAccess.Read.ToString());

try

{

System.IO.File.OpenRead(path);

}

catch (Exception e)

{

MessageBox.Show(e.ToString());

}

}

I have 50 files in arr....i want to check each and every file if any one of them is opened then it thow only exception and move to read another file..

if you are not getting then tell me i'll explain in brief what i want..

thanks





Re: .NET Base Class Library How to check file is open or not

ahmedilyas

firstly, you should be using the specific exceptions, not the generic exception unless you absolutely have to.

Move the System.IO.StreamReader line INSIDE the try block. In addition, you are trying to open the file twice...you should only need to open it once, so remove the System.IO.File.Open() method.

What problems are you having






Re: .NET Base Class Library How to check file is open or not

Situ

System.IO.StreamReader sr = new System.IO.StreamReader(filepath);//FileMode.Open.ToString(), FileAccess.Read.ToString());

try

{

File.OpenRead(filepath);

}

catch (Exception e)

{

MessageBox.Show(e.ToString());

}

its not throwing error..





Re: .NET Base Class Library How to check file is open or not

ahmedilyas

Well in which case it's fine - can you explain the problem you have with the code above or explain what you expect for it to do

The code I had originally posted does what you want it to do - just like that. Have you tried it What happens






Re: .NET Base Class Library How to check file is open or not

Situ

if i define streamreader object inside the try block its not accessible outside ..and i nees to access sr object

so is it other way to check that file is opened or not..instead of open method...





Re: .NET Base Class Library How to check file is open or not

ahmedilyas

you shouldnt need to access it outside the foreach block if you are reading files for each file, what exactly are you trying to do

if you need to access it outside, then define the variable outside:

StreamReader sr;

foreach(string file in ArrayOfFile)

{

try

{

sr = new StreamReader(file);

//do stuff

}

catch (UnauthorizedAccessException)

{

}

catch(IOException ex)

{

}

}

//now you can access sr since its defined/declared outside the foreach loop






Re: .NET Base Class Library How to check file is open or not

Situ

I did this already...it giving error that "unassigned local variable sr "




Re: .NET Base Class Library How to check file is open or not

ahmedilyas

It's still ok, since you will be using it inside the foreach loop however if it does not fall into the foreach loop then make sure that if you are using the sr variable outside it, that it is not null before using it.

Still don't understand why you wish to use it outside the foreach loop since your processing should be done within the foreach loop.

you could also alter it to get rid of the warning:

StreamReader sr = null;

the above is probably better.