.net File.js(“”)和FileInfo有什么区别

k4ymrczo  于 11个月前  发布在  .NET
关注(0)|答案(6)|浏览(95)

我在\ProgramFiles(x86)\MyAppFolder中有一个 *.exe文件。
在x86应用程序中,我检查文件是否存在(64位系统)。简单:

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

字符串
结果是:“filelists == false”(文件确实存在)。据我所知,这是虚拟化。这个问题描述了here它的确定。但下一个代码:

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


“fileThreeThreeThreeThreeThreeThreeThreeThreeThreeThreeThreeThree”
为什么第一种情况和第二种情况的结果不同?

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);
}


//工作良好

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


//不工作

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


谢谢你,谢谢

wbgh16ku

wbgh16ku1#

这是唯一的区别,它与FileInfo的性质有关:

FileInfo fileInfo = new FileInfo("myFile.txt"); // non-existent file
Console.WriteLine(fileInfo.Exists);             // false
File.Create("myFile.txt");
Console.WriteLine(File.Exists("myFile.txt"));   // true
Console.WriteLine(fileInfo.Exists);             // false

字符串
因此,正如您所看到的,fileInfo.Exists的值在您第一次使用它时被缓存。
除此之外,他们在幕后做同样的事情。

vd2z7a6w

vd2z7a6w2#

没有区别,这些方法在.NET Framework中使用完全相同的内部helper方法。您可以在反编译器或引用源代码中看到,helper方法名称为File.FillAttributeInfo()。
在.NET Framework中有这样的重复是非常不寻常的,有不止一种方法来完成同样的事情并不是一件好事。然而,File类是特殊的,它是在.NET 1.0发布时进行a usability study之后添加的。测试主题只有基本的BCL类,如FileStream和FileInfo,除此之外,只有MSDN文档可用。测试结果不是很好,File类被添加到帮助程序员成功编写非常基本的文件操作代码的坑中。如File. ReadSts()和File.ReadAllLines()。
所以这与类没有任何关系,你只是用错了它们。比如没有使用相同的路径。不要轻易使用正斜杠,反斜杠的Map发生在Windows内部,在其他代码中实现不一致。使用//当然不会达到你希望的效果。

pcww981p

pcww981p3#

File.Exists()new FileInfo().Exists在完整路径(目录名+文件名)较长时的行为差异:

var f = @"C:\Program Files (x86)\MyAppFolder\many_subfolders\manager.exe";

//f.length > 260 characters

bool fileExists = File.Exists(f); //return false, even if the file exists

// Throw exception: "The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters."
bool fileInfoExists = new FileInfo(f).Exists;

字符串

7cwmlq89

7cwmlq894#

我刚刚发现这个线程,并希望更新它,因为我有一个问题与FileInfo与File.js。
让我们假设一个场景,在这个场景中,我们为一个当前不存在于Windows共享中的文件创建一个fileinfo对象。

bool fileInfo  = new FileInfo(@"\\uncshare\name\filename.txt");

字符串
在这一点上,文件不存在,但如果我使用另一个工具(其他代码或我的应用程序外部)创建它,然后这样做.

fileInfo.Refresh();
bool exists = fileInfo.Exists;


结果为false,它不存在,即使它存在并且fileInfo已刷新。
为了得到正确的结果,需要...

bool exists = File.Exists(f);


希望能帮助到别人。

gijlo24d

gijlo24d5#

我已经使用下面的Linqpad脚本复制了您的场景

var f = @"C:\Program Files (x86)\MyAppFolder\manager.exe";

bool fileExists = File.Exists(f);
bool fileInfoExists = new FileInfo(f).Exists;

fileExists.Dump();
fileInfoExists.Dump();

字符串
在文件存在和不存在的情况下都运行这个命令,每次都产生相同的输出。也许在你的系统上试试这个命令,看看你是否仍然看到差异。

uajslkp6

uajslkp66#

在第一种情况下,文件路径不正确,您需要在"Program Files (x86)"中添加空格。
其次,Path.合并将返回一个Directory路径,因此您最终将得到类似"C:\Program Files (x86)\MyAppFolder\Manager.exe\"的东西,因此这是一个坏主意。
这两种方法的工作方式相同,因此请确保检查路径是否正确。

相关问题