Filestream not closing!!!

L

lh

I'm having problems with the filestream object not releasing its grubby
grips on a file that i am using it to read the contents of. The application
is working for the first file that i drop into the directory, but the second
file always causes an error. The error is a file.io.exception "file already
being used..." error. The funny thing is that each file is a new file. My
code is below. I'm hoping that someone has seen this issue before and knows
how to solve it. It is whooping my butt.

thanks,
Lyndon


---------------------------------------------------------
using System;
using System.Diagnostics;
using System.IO;
using Valid.Common;
using System.Text.RegularExpressions;

namespace Valid.FileWatcher
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class FTPFileWatcher
{
private static string watchedDirectoryOutput;
private static string fileContents;
private static BooleanSwitch boolSwitch;
private static FileSystemWatcher watcher;
public static FileStream fileStream;


/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
boolSwitch= new BooleanSwitch("OutputSwitch", "output bool switch");
Console.WriteLine("filewatcher = " +
WatchDirectoryForChanges("c:\\testScans"));
Console.WriteLine("press enter to exit");
Console.ReadLine();
}



/// <summary>
/// i watch a given directory for changes and send a string as alert when
something happens with the directoy
/// </summary>
/// <returns></returns>
public static string WatchDirectoryForChanges(string path)
{

//Create a filewatcher object and tell it watch a certain directory
watcher = new FileSystemWatcher();

watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Attributes
| NotifyFilters.LastAccess | NotifyFilters.LastWrite |
NotifyFilters.Security | NotifyFilters.Size;
watcher.Path= path;
watcher.Filter ="*.txt";
watcher.IncludeSubdirectories = false;

//watcher.Changed += new
FileSystemEventHandler(OnWatchedDirectoryChanged);
watcher.Created += new FileSystemEventHandler(OnWatchedDirectoryChanged);
watcher.EnableRaisingEvents = true;

Trace.WriteLineIf(boolSwitch.Enabled,"up top" + watchedDirectoryOutput);
//watcher.Dispose();
return watchedDirectoryOutput;
}

/// <summary>
/// i return the contents of a file
/// </summary>
/// <returns></returns>
public static string ReadFileContents(string filePath){
//open file

Console.WriteLine("$$$$$$$$$$$$$$$$$$$$$$$$$$$in readFileContents");
fileStream = new FileStream(filePath,FileMode.Open,FileAccess.Read);
if(fileStream.CanSeek){

string fileName = fileStream.Name;
fileStream.Close();

fileStream = new FileStream(fileName,FileMode.Open,FileAccess.Read);
Console.WriteLine("****************CAN SEEK");
}
if(!fileStream.CanRead){
// close it and reopen for read
string fileName = fileStream.Name;
fileStream.Close();

fileStream = new FileStream(fileName,FileMode.Open,FileAccess.Read);

}
StreamReader streamReader = new StreamReader(fileStream);
string contents = streamReader.ReadToEnd();

fileStream.Close();
streamReader.Close();

return contents;
}




private static void OnWatchedDirectoryChanged(object source,
FileSystemEventArgs e)
{
Console.WriteLine("FULLPATH = " + e.FullPath);
Console.WriteLine("ChangeType = " + e.ChangeType);

fileContents = ReadFileContents(e.FullPath);

}



}
}
 
R

Robert Jordan

lh said:
I'm having problems with the filestream object not releasing its grubby
grips on a file that i am using it to read the contents of. The application
is working for the first file that i drop into the directory, but the second
file always causes an error. The error is a file.io.exception "file already
being used..." error. The funny thing is that each file is a new file. My
code is below. I'm hoping that someone has seen this issue before and knows
how to solve it. It is whooping my butt.

You have to wait until the file has been completely
written and closed by the process that created the file.

BTW, because you have a console app the FileSystemWatcher
you created is asynchonous: it calls OnWatchedDirectoryChanged
on another thread. Don't get confused by that.

private static void OnWatchedDirectoryChanged(object source,
FileSystemEventArgs e)
{
Console.WriteLine("FULLPATH = " + e.FullPath);
Console.WriteLine("ChangeType = " + e.ChangeType);

while (true) {
try {
fileContents = ReadFileContents(e.FullPath);
break;
}
catch (IOException) {
Thread.Sleep(1000)
}
}
 
H

hOSAM

FileSystemWatcher has a kinda funny behavior, maybe you are suffering from
it. The on file change event could fire more than once, which means that you
could be trying to acces the file more than once while it is still open.

take a look at this might be a help:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/
vbtbsTroubleshootingUNCPathNamesNotAcceptedOnNT4Machines.asp

lh said:
I'm having problems with the filestream object not releasing its grubby
grips on a file that i am using it to read the contents of. The application
is working for the first file that i drop into the directory, but the second
file always causes an error. The error is a file.io.exception "file already
being used..." error. The funny thing is that each file is a new file. My
code is below. I'm hoping that someone has seen this issue before and knows
how to solve it. It is whooping my butt.

thanks,
Lyndon


---------------------------------------------------------
using System;
using System.Diagnostics;
using System.IO;
using Valid.Common;
using System.Text.RegularExpressions;

namespace Valid.FileWatcher
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class FTPFileWatcher
{
private static string watchedDirectoryOutput;
private static string fileContents;
private static BooleanSwitch boolSwitch;
private static FileSystemWatcher watcher;
public static FileStream fileStream;


/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
boolSwitch= new BooleanSwitch("OutputSwitch", "output bool switch");
Console.WriteLine("filewatcher = " +
WatchDirectoryForChanges("c:\\testScans"));
Console.WriteLine("press enter to exit");
Console.ReadLine();
}



/// <summary>
/// i watch a given directory for changes and send a string as alert when
something happens with the directoy
/// </summary>
/// <returns></returns>
public static string WatchDirectoryForChanges(string path)
{

//Create a filewatcher object and tell it watch a certain directory
watcher = new FileSystemWatcher();

watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Attributes
| NotifyFilters.LastAccess | NotifyFilters.LastWrite |
NotifyFilters.Security | NotifyFilters.Size;
watcher.Path= path;
watcher.Filter ="*.txt";
watcher.IncludeSubdirectories = false;

//watcher.Changed += new
FileSystemEventHandler(OnWatchedDirectoryChanged);
watcher.Created += new FileSystemEventHandler(OnWatchedDirectoryChanged);
watcher.EnableRaisingEvents = true;

Trace.WriteLineIf(boolSwitch.Enabled,"up top" + watchedDirectoryOutput);
//watcher.Dispose();
return watchedDirectoryOutput;
}

/// <summary>
/// i return the contents of a file
/// </summary>
/// <returns></returns>
public static string ReadFileContents(string filePath){
//open file

Console.WriteLine("$$$$$$$$$$$$$$$$$$$$$$$$$$$in readFileContents");
fileStream = new FileStream(filePath,FileMode.Open,FileAccess.Read);
if(fileStream.CanSeek){

string fileName = fileStream.Name;
fileStream.Close();

fileStream = new FileStream(fileName,FileMode.Open,FileAccess.Read);
Console.WriteLine("****************CAN SEEK");
}
if(!fileStream.CanRead){
// close it and reopen for read
string fileName = fileStream.Name;
fileStream.Close();

fileStream = new FileStream(fileName,FileMode.Open,FileAccess.Read);

}
StreamReader streamReader = new StreamReader(fileStream);
string contents = streamReader.ReadToEnd();

fileStream.Close();
streamReader.Close();

return contents;
}




private static void OnWatchedDirectoryChanged(object source,
FileSystemEventArgs e)
{
Console.WriteLine("FULLPATH = " + e.FullPath);
Console.WriteLine("ChangeType = " + e.ChangeType);

fileContents = ReadFileContents(e.FullPath);

}



}
}
 
L

lh

thanks Robert. The fileSystemWatcher class is a tricky b*stard. I was
looking for a true way to solve the problem, but your solution effectively
patches it up-- solving my problem. Now I can finally go to bed. I really
appreciate your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top