FileSystemEventArgs

P

Peter Kirk

I am still "playing" with FileSystemWatcher, and have a question regarding
FileSystemEventArgs. For example, the following method is called when a file
is created:

private void OnCreated(object source, FileSystemEventArgs e)
{
Console.WriteLine("OnCreated: FullPath=" + e.FullPath + "; Name=" +
e.Name);
}

But, case-sensitivity is all up the spout. If I create a file called "my
FILE.txt", then the FileSystemEventArgs will not detect the different cases,
and simply report a file called "my file.txt".

Is there any way to alter this behaviour so that FileSystemEventArgs
respects the case of the name of the created file.

Thanks,
Peter
 
P

Peter Kirk

Hi, thanks for the reply.

I am making an application which logs changes in a directory and creates a
report every so often. The users who create/update files like to be able to
see the same filename case in the report I generate - so in that sense it
does matter. (I also log "rename" events - and if I am not mistaken I get
events like "my file.txt" renamed to "my file.txt" - when actually it was
"MY FILE.txt" renamed to "My File.txt", and maybe that matters to the
users...)

It also matters to me in the sense that it seems sloppy not to use the exact
case from the filesystem - then it can be up to my application to "lower" or
"upper" case it as I see fit. Now I don't get the chance.

You are right that the windows file system will not allow two files whose
names differ only in case, but somewhere the file system does "care" about
case because it can present file names in different cases in windows
explorer.

I have noticed that DirectoryInfo.GetFiles(directoryName) returns an array
of FileInfo objects which do respect the case of the filenames in the
filesystem. So some bits of .NET do care about filename case, others do not.
Irritating.

Thanks,
Peter
 
M

Moty Michaely

Hey Peter,

Since file system doesn't care about case sensitivity of files or folders,
so doe's FileSstemWatcher.

Therefor, I don't think there is a way of altering this point.

Why doe's it matter to you on CreateFile event?

- Moty -
 
M

Moty Michaely

Hey,

Well I guess you have your reasons :).

About rename event I am not so sure an event will raise if a file was
renamed just by it's case. Worths to check.

Anyhow, you can get the created file info as you described, this probably
would solve your problem since there is not any way to get the file's name
in the right case with FileSystemEventArgs :(.

-Moty-
 
P

Peter Kirk

Moty Michaely said:
Well I guess you have your reasons :).

About rename event I am not so sure an event will raise if a file was
renamed just by it's case. Worths to check.

Anyhow, you can get the created file info as you described, this probably
would solve your problem since there is not any way to get the file's name
in the right case with FileSystemEventArgs :(.

A rename event is indeed fired if it is only the case of the filename which
is altered.

Anyway, if anyone is interested, a "workaround" I found for my problem is:

private void OnCreated(object source, FileSystemEventArgs e)
{
Console.WriteLine("OnCreated: FullPath=" + e.FullPath + "; Name=" +
e.Name);
FileInfo fi = new FileInfo(e.FullPath);
if (fi.Exists)
{
FileInfo[] fis = fi.Directory.GetFiles(fi.Name);
Console.WriteLine(fis[0].FullName);
}
}

That is, fis[0] contains a FileInfo object which contains a filename with
the same case as the file on the filesystem.

Peter
 

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