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);
}
}
}
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);
}
}
}