如何在.NET/C#中创建带尾随点(.)的文件夹?

IT *_*DAV 2 c# ntfs create-directory .net-4.5

我需要使用C#创建带尾随点(.)的文件夹.这是我的代码:

System.IO.Directory.CreateDirectory("d:\\MyCorp Inc.");
Run Code Online (Sandbox Code Playgroud)

但是创建的文件夹没有最后一个点.有解决方案吗?

我使用的是.NET Framework 4.5/C#.我的文件系统是Windows 8上的NTFS.

its*_*e86 6

微软明确指出,这是不应该做的事情:http://msdn.microsoft.com/en-us/library/aa365247%28VS.85%29.aspx

不要使用空格或句点结束文件或目录名称.虽然底层文件系统可能支持此类名称,但Windows shell和用户界面却不支持.但是,可以将句点指定为名称的第一个字符.例如,".temp".


Mit*_*tch 5

这不是 .NET 的限制,而是Win32 API 的限制。尝试在命令 shell 中创建一个:

\n\n
c:\\drop>mkdir test.\n\nc:\\drop>dir\n Volume in drive C has no label.\n Volume Serial Number is C0F3-338B\n\n Directory of c:\\drop\n\n2013-04-02  17:54    <DIR>          .\n2013-04-02  17:54    <DIR>          ..\n2013-04-02  17:54    <DIR>          test\n
Run Code Online (Sandbox Code Playgroud)\n\n

有关命名建议,请参阅MSDN 。去引用:

\n\n
\n

\xe2\x80\xa2 不要以空格或句点结尾文件或目录名称。\n 尽管底层文件系统可能支持此类名称,但 Windows shell 和用户界面不支持。但是,可以将句点指定为名称的第一个字符。例如,\n“.temp”。

\n
\n\n

如果您确实非常坚持使用尾随点,请使用\\\\?\\C:\\path.

\n\n
c:\\drop>mkdir \\\\?\\c:\\drop\\test2.\n\nc:\\drop>dir\n Volume in drive C has no label.\n Volume Serial Number is C0F3-338B\n\n Directory of c:\\drop\n\n2013-04-02  17:58    <DIR>          .\n2013-04-02  17:58    <DIR>          ..\n2013-04-02  17:54    <DIR>          test\n2013-04-02  17:58    <DIR>          test2.\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

编辑:内容中列出的要求是不创建其他文件,因此不要使用文件。您可以在目录上创建一个“名称”流并在其中存储数据。请参阅以下示例:

\n\n
c:\\drop>mkdir "Example Company Name"\nc:\\drop>notepad "Example Company Name:name.txt"\nc:\\drop>dir\n Volume in drive C has no label.\n Volume Serial Number is C0F3-338B\n\n Directory of c:\\drop\n\n2013-04-02  18:25    <DIR>          .\n2013-04-02  18:25    <DIR>          ..\n2013-04-02  18:25    <DIR>          Example Company Name\n               0 File(s)              0 bytes\n               3 Dir(s)  77,336,379,392 bytes free\nc:\\drop>dir "Example Company Name"\n Volume in drive C has no label.\n Volume Serial Number is C0F3-338B\n\n Directory of c:\\drop\\Example Company Name\n\n2013-04-02  18:25    <DIR>          .\n2013-04-02  18:25    <DIR>          ..\n               0 File(s)              0 bytes\n               2 Dir(s)  77,336,313,856 bytes free\n
Run Code Online (Sandbox Code Playgroud)\n\n

或者在 .NET 中:

\n\n
class Program\n{\n    static void Main(string[] args)\n    {\n        string companyName = "Example Company?*, Inc.";\n\n        //Example Company Inc\n        var sanitizedName = sanitize(companyName);\n\n        //Create the directory\n        Directory.CreateDirectory(sanitizedName);\n\n        //Create the name store\n        var nameStreamPath = sanitizedName + ":Name";\n        writeFileContent(companyName, nameStreamPath);\n\n        //Try to return the name\n        Console.WriteLine(getFileContent(nameStreamPath));\n        Console.ReadLine();\n    }\n\n    private static string getFileContent(string path)\n    {\n        using (var sr = new StreamReader(new FileStream(\n            // we have to call CreateFile directly to avoid overzealous path validation\n            NativeMethods.CreateFileOrFail(path, false), FileAccess.Read)))\n        {\n            return sr.ReadToEnd();\n        }\n    }\n\n    private static void writeFileContent(string companyName, string path)\n    {\n        using (var sw = new StreamWriter(new FileStream(\n            // We have to call CreateFile directly to avoid overzealous path validation\n            NativeMethods.CreateFileOrFail(path, true), FileAccess.Write)))\n        {\n            sw.Write(companyName);\n        }\n    }\n\n    private static string sanitize(string path)\n    {\n        char[] newPath = new char[path.Length];\n        int newPathLoc = 0;\n        for (int i = 0; i < path.Length; i++)\n        {\n            if (char.IsLetter(path[i]) || char.IsDigit(path[i]))\n            {\n                newPath[newPathLoc] = path[i];\n                newPathLoc++;\n            }\n        }\n        return new string(newPath, 0, newPathLoc);\n    }\n}\n\nclass NativeMethods\n{\n    [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]\n    private static extern SafeFileHandle CreateFile(\n        string fileName,\n        [MarshalAs(UnmanagedType.U4)] FileAccess fileAccess,\n        [MarshalAs(UnmanagedType.U4)] FileShare fileShare,\n        IntPtr securityAttributes,\n        [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,\n        [MarshalAs(UnmanagedType.U4)] FileAttributes flags,\n        IntPtr template);\n\n    public static SafeFileHandle CreateFileOrFail(string path, bool write)\n    {\n        var handle = CreateFile(path,\n            write ? FileAccess.Write : FileAccess.Read,\n            write ? FileShare.None : FileShare.Read,\n            IntPtr.Zero,\n            write ? FileMode.Create : FileMode.Open,\n            FileAttributes.Normal,\n            IntPtr.Zero);\n\n        if (handle.IsInvalid)\n            throw new System.ComponentModel.Win32Exception();\n\n        return handle;\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n