是什么File.Exists之间的差异(QUOT;")和FileInfo的存在 [英] What is difference between File.Exists("") and FileInfo exists

查看:187
本文介绍了是什么File.Exists之间的差异(QUOT;")和FileInfo的存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在\ PROGRAMFILES(x86)的\ MyAppFolder。

I have an *.exe file in \ProgramFiles(x86)\MyAppFolder.

在86应用程序,我检查文件是否存在(64位系统)。 简单的:

In x86 application I check if the file exists (64 bit system). simple:

bool fileExists = File.Exists(@"\ProgramFiles(x86)\MyAppFolder\Manager.exe");

其结果是: FILEEXISTS ==假(该文件确实存在)。这是虚拟化,因为我understand.That出具说明<一href="http://stackoverflow.com/questions/2160699/check-if-file-exist-on-64-bits-system-using-file-exists">here好的。 但是,接下来的code:

The result is: "fileExists == false" (the file is really there). It's Virtualization as I understand.That issue described here Its ok. But next code:

bool fileExists = new FileInfo("\\Path").Exists;

FILEEXISTS ==真

"fileExists == true"

为什么结果第1和第2例不同?

Why is the result different in 1st and 2nd cases?

var controller = new ServiceController(Product.ServiceName);
_manager.Enabled = controller.Status == ServiceControllerStatus.Running;

var info = new DirectoryInfo(Assembly.GetExecutingAssembly().Location);

var s = File.Exists(@"D:\TFS\GL_SOURCES\Teklynx_LPM\Dev\Server\Debug\Manager.exe");

string pathToManager = string.Empty;

if (info.Parent != null)
{
    var pathToModule = info.Parent.FullName;
    pathToManager = Path.Combine(pathToModule,"Manager.exe").Replace(" ",string.Empty);
}

//做工不错

//works good

var fileInfo = new FileInfo(pathToManager);
var managerSeparator = new ToolStripSeparator()
{
    Visible = _manager.Visible = fileInfo.Exists // true
};

//不行的。

//Does not work

var managerSeparator = new ToolStripSeparator()
{
    Visible = _manager.Visible = File.Exists(pathToManager ) // false
};

谢谢!

推荐答案

没有区别,这些方法都使用.NET框架内完全相同的内部助手方法。有些东西你可以用反编译或参考源源$ C ​​$ C看到,辅助方法名是File.FillAttributeInfo()。

There is no difference, these methods use the exact same internal helper method inside the .NET Framework. Something you can see with a decompiler or the Reference Source source code, the helper method name is File.FillAttributeInfo().

有在.NET Framework中重复这样是pretty的不寻常的,不完全是一件好事,必须完成同样的事情不止一种方法。 File类不过是特殊的,它得到了前.NET 1.0运进行可用性研究后添加。考试科目刚刚有了基本的BCL类的工作,喜欢的FileStream和FileInfo的,否则只有MSDN文档。测试结果不太好,File类得到了补充,以帮助程序员倒在成功的写作非常基本的文件操作code坑。像File.Exists()和File.ReadAllLines()。

Having duplication like this in the .NET Framework is pretty unusual, not exactly a Good Thing to have more than one way to accomplish the same thing. The File class is however special, it got added after a usability study conducted before .NET 1.0 shipped. The test subjects just had the basic BCL classes to work with, like FileStream and FileInfo, and otherwise only had MSDN documentation available. The test results were not very good, the File class got added to help programmers fall in the pit of success writing very basic file manipulation code. Like File.Exists() and File.ReadAllLines().

所以,它没有任何关系的类,你只是用他们错了。像没有实际使用相同的路径。不要去容易对正斜杠,映射到反斜杠发生在Windows里,并在其他code并不一致。使用//当然不会做你希望它做。

So it doesn't have anything to do with the classes, you are just using them wrong. Like not actually using the same path. Do go easy on the forward slashes, the mapping to backward slashes happens inside Windows and is inconsistently implemented in other code. Using // certainly doesn't do what you hope it does.

这篇关于是什么File.Exists之间的差异(QUOT;&QUOT;)和FileInfo的存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆