By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,077 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,077 IT Pros & Developers. It's quick & easy.

File.Exists case change

P: n/a
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?
Nov 14 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
DH
Sweetiecakes wrote:
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
}
Nov 14 '08 #2

P: n/a
DH wrote:
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
Nov 14 '08 #3

P: n/a
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
Nov 14 '08 #4

P: n/a
DH
Sweetiecakes wrote:
DH wrote:
>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
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.
Nov 14 '08 #5

P: n/a
"Sweetiecakes" <x@x.comwrote in message
news:49***********************@news.fv.fi...
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.
Nov 14 '08 #6

P: n/a
DH
Sweetiecakes wrote:
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.
Nov 14 '08 #7

P: n/a
Sweetiecakes wrote:
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.GetDirectoryN ame(fnm)), s =s ==
Path.GetFullPath(fnm));
}

}
Arne
Nov 15 '08 #8

P: n/a
MC

"Arne Vajhøj" <ar**@vajhoej.dkwrote in message news:49***********************@news.sunsite.dk...
Sweetiecakes wrote:
>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.
Right -- to underscore that point -- Windows NTFS is *not* a case-sensitive file system and you will find discrepancies from one machine to another as to how case is handled. Under Windows, if test.txt exists, then so does Test.Txt, because they are the same file.
Nov 15 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.