简体   繁体   English

C#无法打开名为CON.DE.txt的文件?

[英]C# cannot open file named CON.DE.txt?

I encountered a strange issue in C# when i tried to open a txt file named CON.txt. 当我尝试打开名为CON.txt的txt文件时,在C#中遇到一个奇怪的问题。 I wanted to store yahoo finance data for the CON.DE symbol. 我想为CON.DE符号存储雅虎财务数据。

using System.IO;

namespace Test
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            File.Open("CON.DE.txt", FileMode.Create);
        }
    }
}

Visual Studio throws an exception and says: Visual Studio引发异常并说:

FileStream will not open Win32 devices such as disk partitions and tape drives. FileStream将不会打开Win32设备,例如磁盘分区和磁带驱动器。 Avoid use of "\\.\\" in the path. 避免在路径中使用“ \\。\\”。

Is this a bug in .NET Framework or Win32? 这是.NET Framework还是Win32中的错误? Is there a way around this? 有没有解决的办法?

It would appear that CON is a keyword reserved by Windows, harking back to the days of MS-DOS (but remaining reserved to this day). 看起来, CON是Windows保留的关键字,可以追溯到MS-DOS的日子(但保留至今)。

One solution would be to replace the full stop between 'CON' and 'DE' with another character (eg CON-DE.txt or equivalent). 一种解决方案是用另一个字符(例如CON-DE.txt或同等字符)替换'CON'和'DE'之间的CON-DE.txt

See: https://support.microsoft.com/en-gb/help/74496/ms-dos-device-driver-names-cannot-be-used-as-file-names for a complete list of reserved MS-DOS device driver names. 有关保留的MS-DOS的完整列表,请参阅: https : //support.microsoft.com/zh-cn/help/74496/ms-dos-device-driver-names-cannot-be-used-as-file-names设备驱动程序名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM