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

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

问题描述

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

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

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

我是否遗漏了什么,或者这是 .NET 和/或 Windows 中的错误?

各自的代码:

使用系统;使用 System.Collections.Generic;使用 System.IO;使用 System.Linq;使用 System.Text;使用 System.Threading;使用 System.Threading.Tasks;命名空间 throw_test{静态类程序{静态无效主(){int fileCount1 = Directory.GetFiles("D:\MyDir", "*.xlsx").Length;int fileCount2 = Directory.GetFiles("D:\MyDir", "*.xls").Length;Console.WriteLine("文件数为 1:" + fileCount1);Console.WriteLine("文件数 2:" + fileCount2);Console.Read();}}}

编辑 1

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

  • dir *.xlsx 按预期返回所有文件
  • dir *.xls 返回找不到文件"

Windows 7 返回上述两个命令的预期文件.

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

解决方案

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

<块引用>

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

Windows 8.1 的行为对我来说不可重现.我刚刚在我的 Windows 8.1 x64 机器上运行了一个测试,它符合预期的 Windows 7 行为.我会检查机器上的当前文件夹是否正确.

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

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

<块引用>

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

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

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.

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

The respective code:

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

Edit 1

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

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

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

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

解决方案

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

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.

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.

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天全站免登陆