5

As title says I don't know what's wrong with my code but if (File.Exists) give negative result even if the file is there.

Below is my code

if (File.Exists(ZFileConfig.FileName.Replace(".xml", "_abc.xml")))

Here, ZFileConfig.FileName is E:\\Application\\Application\\bin\\Debug\\resources\\FirstFile.xml

And amazingly ZFileConfig.FileName.Replace(".xml", "_abc.xml") gives me E:\\Application\\Application\\bin\\Debug\\resources\\FirstFile_abc.xml that is what is needed. EVENTHOUGH IF falied to return TRUE.

enter image description here

enter image description here

Rob
  • 26,989
  • 16
  • 82
  • 98
DhavalR
  • 1,409
  • 3
  • 29
  • 57
  • So you are saying that `File.Exists` says that the file doesn't exist, even if it does? Are you SURE that the file 'FirstFile_abs.xml' exists? Can you post a screen shot? – rhughes Feb 04 '14 at 05:06
  • What is the return value of ZFileConfig.FileName.Replace()? Start there. You may be making an assumption about what is actually returned. – Paul Sasik Feb 04 '14 at 05:08
  • Does your application have permissions to access the directory? – HTX9 Feb 04 '14 at 05:08
  • You shouldn't use `File.Exists` anyway. Even if it returns true now, the file could be deleted before you open it. Just try to open and handle the exception. – Ben Voigt Feb 04 '14 at 05:11

2 Answers2

9

It looks like your file is actually named abc_RotateFlip.xml.xml.

I can't imagine why any programmer would ever allow hidden file extensions, but your Excel file shows that they are indeed hidden. Turn that off! Choose to know what's going on inside your computer!

enter image description here

You can also use this registry script to change that setting;

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced]
"HideFileExt"=dword:00000000
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • I too think so. But I want to ask you one thing. Here this `.xml` file is made from DataSet.WriteToXml() where WriteToXml() is UDF to make a `.xml` file. I am making two files `dsLabelFileConfig.WriteXml(info.Directory + @"\" + info.Name + ".xml");` and `dsRotateFlip.WriteXml(info.Name.Replace(".xml" , "_RotateFlip.xml"));`. But first is nicely made. Why second makes problems.? – DhavalR Feb 04 '14 at 05:32
  • @DhavalR: If the replace is doing something, then `info.Name` already ends in `.xml`. And then `info.Name + ".xml"` ends in `.xml.xml` – Ben Voigt Feb 04 '14 at 05:33
  • `dsLabelFileConfig` is good. But `dsRotateFlip` is making problem. – DhavalR Feb 04 '14 at 05:35
  • @DhavalR: I don't know because I don't have all your code. Maybe another `.xml` is added inside `WriteXML`. Maybe you or a coworker saw the file in Explorer without .xml on the end, because it was hidden, and added it. Surely you can debug through the file creation and see if it uses the wrong name. – Ben Voigt Feb 04 '14 at 05:37
3

Please check with FileInfo :

FileInfo fi = new FileInfo(@"_abc.xml");
bool isExists = fi.Exists;

Generally if you are performing a single operation on a file, use the File class. If you are performing multiple operations on the same file, use FileInfo.

The reason to do it this way is because of the security checking done when accessing a file. When you create an instance of FileInfo, the check is only performed once. However, each time you use a static File method the check is performed.

Community
  • 1
  • 1
  • +1 Good call. It's also good practice to clarify your code when encountering seemingly strange behavior where each constituent operation is a separate line of code and assignment to a temp variable. It gets verbose but allows you to debug each call individually rather than two or more at once that are folded into a single line of code. – Paul Sasik Feb 04 '14 at 05:11
  • Hm, can you explain it? How can a chek for "_abc.xml" solve his problem? and why `fi.Exist` is better then `File.Exists`? – JleruOHeP Feb 04 '14 at 05:11
  • @Shahrooz jefri: Tried that too. But still `false`. – DhavalR Feb 04 '14 at 05:13
  • @ShahroozJefriㇱ thanks, but what about "_abc.xml" part? His file should be "somethingold_abc.xml" – JleruOHeP Feb 04 '14 at 05:15
  • I tried both the ways using `FileInfo` and `File Class`. But what is happening.? – DhavalR Feb 04 '14 at 05:18