File.Exists case change

S

Sweetiecakes

In our series of "probably something simple"...

I am doing a File.Exists operation to check if a file exists. Let's
assume that this file is C:/test.txt.

Now, if I do File.Exists("C:\\Test.txt"), I do not want it to tell me
that the file exists - this is a check for a File.Move operation (some
file renaming functionality). But if I check C:\\test.txt, it should
return true.

Any suggestions?
 
D

DH

Sweetiecakes said:
In our series of "probably something simple"...

I am doing a File.Exists operation to check if a file exists. Let's
assume that this file is C:/test.txt.

Now, if I do File.Exists("C:\\Test.txt"), I do not want it to tell me
that the file exists - this is a check for a File.Move operation (some
file renaming functionality). But if I check C:\\test.txt, it should
return true.

Any suggestions?

Sweetiecakes,
the question does not make mush sense you are saying you dont want it to
tel you if the file esists but the it should return true;
File.Exists() returns a boolean (true or false) if the file exists it
returns true otherwise false.

if you want to check a folder to make sure a file with that name does
not exist you could do:
if(!File.Exists(destpath)){
//move
}
else{
//do whatever you want
}
 
S

Sweetiecakes

DH said:
Sweetiecakes,
the question does not make mush sense you are saying you dont want it to
tel you if the file esists but the it should return true;
File.Exists() returns a boolean (true or false) if the file exists it
returns true otherwise false.

if you want to check a folder to make sure a file with that name does
not exist you could do:
if(!File.Exists(destpath)){
//move
}
else{
//do whatever you want
}

I don't know what I've been typing in my last message - but basically,
what I meant, was that I want the File.Exists() command to be
_case-sensitive_, so that test.txt and Test.txt are not the same thing
(such as in an Unix environment).

Thanks
 
S

Sweetiecakes

I don't know what I've been typing in my last message - but basically,
what I meant, was that I want the File.Exists() command to be
_case-sensitive_, so that test.txt and Test.txt are not the same thing
(such as in an Unix environment).

Thanks
 
D

DH

Sweetiecakes said:
I don't know what I've been typing in my last message - but basically,
what I meant, was that I want the File.Exists() command to be
_case-sensitive_, so that test.txt and Test.txt are not the same thing
(such as in an Unix environment).

Thanks
That is not going to be possible in a windows environment. In windows
test.txt and Test.txt are the same thing. so unless ur using a *nix
environment in windows you will never have test.txt and Test.txt in the
same directory.
 
J

Jeff Johnson

I don't know what I've been typing in my last message - but basically,
what I meant, was that I want the File.Exists() command to be
_case-sensitive_, so that test.txt and Test.txt are not the same thing
(such as in an Unix environment).

I would say you should try to create a FileInfo object and then (assuming
Exists returns true) test its Name property against your original string in
a case-sensitive manner. This is off the top of my head, and I can't be
positive that the FileInfo object will return the properly-cased file name
in the Name property or if it will simply parrot what you put in, but my gut
feeling is that you'll get the real file name. Try it.
 
D

DH

Sweetiecakes said:
I don't know what I've been typing in my last message - but basically,
what I meant, was that I want the File.Exists() command to be
_case-sensitive_, so that test.txt and Test.txt are not the same thing
(such as in an Unix environment).

Thanks
I guess if you really want to make it case sensitive you could use
Directory.GetFiles()
and go through the files and check them but this is going to be slow and
inefficient if the folder has a lot of files.
 
A

Arne Vajhøj

Sweetiecakes said:
I don't know what I've been typing in my last message - but basically,
what I meant, was that I want the File.Exists() command to be
_case-sensitive_, so that test.txt and Test.txt are not the same thing
(such as in an Unix environment).

I am very sure that trying to use NTFS as a case sensitive file system
will be a disaster.

But if you insist, then try:

public static class MyFile
{
public static bool MyExists(string fnm)
{
if(!File.Exists(fnm)) return false;
return
Array.Exists(Directory.GetFiles(Path.GetDirectoryName(fnm)), s => s ==
Path.GetFullPath(fnm));
}

}


Arne
 

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