File Watcher returns LOWER CASE only.

J

jimdscudder

I am using a file watcher to watch for files changing. My problem is: the
string file info returned is all LOWER CASE.

Below is the important part of the code?

Any suggestions?



using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.ServiceProcess;

using System.IO;



using System.Runtime.InteropServices;



private void InitializeComponent()

{

this.fileSystemWatcher1 = new
System.IO.FileSystemWatcher();

((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).BeginInit();

//

// fileSystemWatcher1

//

this.fileSystemWatcher1.EnableRaisingEvents = true;

this.fileSystemWatcher1.IncludeSubdirectories = true;

this.fileSystemWatcher1.Path = "C:\\";

this.fileSystemWatcher1.Filter = "*.*";

this.fileSystemWatcher1.Deleted += new
System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Deleted);

this.fileSystemWatcher1.Renamed += new
System.IO.RenamedEventHandler(this.fileSystemWatcher1_Renamed);

this.fileSystemWatcher1.Changed += new
System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Changed);

this.fileSystemWatcher1.Created += new
System.IO.FileSystemEventHandler(this.fileSystemWatcher1_Created);



this.CanHandlePowerEvent = true;

this.CanPauseAndContinue = true;

this.CanShutdown = true;

this.ServiceName = "FileSysWatch";

((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).EndInit();



private void fileSystemWatcher1_Changed(object sender,
System.IO.FileSystemEventArgs e)

{

name = e.Name; //??LOWER CASE ONLY!!



}
 
L

Lebesgue

Your problem is that it returns only files which are lower case or makes all
file names lower case?
If it returns name in lower case, it's no problem, file names are not case
sensitive in Windows.
 
J

jimdscudder

The problem is i use this to back up files and I world like the backed up
files and paths to reflect the case they were first in.
 
N

Nick Malik [Microsoft]

jimdscudder said:
The problem is i use this to back up files and I world like the backed up
files and paths to reflect the case they were first in.

I believe that you can get the original case of the filename by creating a
File object using the filename passed in. Not sure, though. Try it.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
 
M

mattmc3

I've been struggling with this too, but the solution is dead simple if
you're willing to reference a COM component from your .NET project.
Here are the steps:

1.) Add a reference to your project to the "Microsoft Scripting
Runtime" COM object


2.) Build the following function:

public string GetCaseSensitivePath(string path) {
Scripting.FileSystemObjectClass fso = new FileSystemObjectClass();
return fso.GetAbsolutePathName(path);
}


3.) Build a helper function to fix your FileSystemEventArgs object:

private FileSystemEventArgs FixFileSystemEventArgs(FileSystemEventArgs
e) {
string path = GetCaseSensitiveFilePath(e.FullPath);
return new FileSystemEventArgs(e.ChangeType,
Path.GetDirectoryName(path), Path.GetFileName(path));
}


This one took me forever to figure out. I even built my own path
parser, but I'll take a pre-built 2 liner from classic VBScript over
maintaining that parser any day, even if it does seem icky to reference
a COM component from C#.

- Matt McElheny
 

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