Directory.GetFiles-不同的输出取决于操作系统 [英] Directory.GetFiles - different output dependent on OS

查看:198
本文介绍了Directory.GetFiles-不同的输出取决于操作系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的程序。它运行.NET 4.5,并在Visual Studio 2013中构建。

I have a simple program. It runs .NET 4.5 and is built in Visual Studio 2013.

D:\\MyDir 已满 .xlsx 文件,没有 .xls 文件。当我在Windows 8.1 x64上运行程序时, *。xls 的筛选器未返回任何结果。当我在Windows 7 x86上运行具有相同.NET版本的相同程序时, *。xls 筛选器返回与 *相同的结果。 xlsx 过滤器。

D:\\MyDir is full of .xlsx files and no .xls files. When I run the program on Windows 8.1 x64, the filter for *.xls returns no results. When I run the same program, with the same .NET version on Windows 7 x86, the *.xls filter returns the same results as the *.xlsx filter.

两个系统上的测试文件夹肯定包含相同的数据。

The test folders on both systems definitely contain the same data.

我是否缺少某些东西,或者这是.NET和/或Windows中的错误?

Am I missing something, or is this a bug in .NET and/or Windows?

相应的代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace throw_test
{
    static class Program
    {
        static void Main()
        {
            int fileCount1 = Directory.GetFiles("D:\\MyDir", "*.xlsx").Length;
            int fileCount2 = Directory.GetFiles("D:\\MyDir", "*.xls").Length;

            Console.WriteLine("File Count 1: " + fileCount1);
            Console.WriteLine("File Count 2: " + fileCount2);

            Console.Read();
        }
    }
}

编辑1

在Windows 8.1 x64中使用命令提示符导航到目录时:

When I navigate to the directory using the command prompt in Windows 8.1 x64:


  • dir * .xlsx 返回所有文件

  • dir * .xls 返回找不到文件

  • dir *.xlsx returns all files as expected
  • dir *.xls returns 'File Not Found'

Windows 7在上述两个命令中均返回期望的文件。

Windows 7 returns the expected files on both of the above commands.

我的猜测是.NET在幕后使用此命令,因此得到了上面的结果?

My guess is that .NET uses this command under the hood, thus the above results?

推荐答案

您的Windows 7行为是设计上的。从关于GetFiles()方法的文档(请参阅下面的第一条注释) 备注部分):

Your Windows 7 behavior is by design. From the documentation on the GetFiles() method (see the first note below the "Remarks" section):


在searchPattern中使用星号通配符(例如 * .txt)时,匹配行为扩展名恰好三个字符长时与扩展名多于或少于三个字符长时不同。文件扩展名恰好为三个字符的searchPattern返回的文件扩展名为三个或更多字符,其中前三个字符与searchPattern中指定的文件扩展名匹配。具有文件扩展名一个,两个或三个以上字符的searchPattern仅返回具有与searchPattern中指定的文件扩展名完全相同长度的扩展名的文件。

When using the asterisk wildcard character in a searchPattern, such as "*.txt", the matching behavior when the extension is exactly three characters long is different than when the extension is more or less than three characters long. A searchPattern with a file extension of exactly three characters returns files having an extension of three or more characters, where the first three characters match the file extension specified in the searchPattern. A searchPattern with a file extension of one, two, or more than three characters returns only files having extensions of exactly that length that match the file extension specified in the searchPattern.

Windows 8.1行为对我来说是不可复制。我刚刚在Windows 8.1 x64计算机上进行了测试,它符合预期的Windows 7行为。我会检查机器上的当前文件夹是否正确。

The Windows 8.1 behavior is a not reproducible for me. I just ran a test on my Windows 8.1 x64 machine, and it matched the expected Windows 7 behavior. I would check that the current folder on the machine is correct.

我也可以通过打开命令提示符,导航到适当的目录并获得相同的结果。键入 dir * .xls 。我希望GetFiles()函数和命令提示符都将搜索模式传递​​到同一个低级操作系统函数中。

I can also get the same results just by opening up a command prompt, navigating to the appropriate directory, and typing dir *.xls. I expect that both the GetFiles() function and the command prompt will pass the search pattern into the same low-level operating system function.

这是在谈论一些不同的问题,但是这篇文章也值得一读:

It's talking about some different issues, but this post is also worth a read:


http://blogs.msdn.com/b/oldnewthing/archive/2007/12/17/6785519.aspx

这篇关于Directory.GetFiles-不同的输出取决于操作系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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