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

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

System.IO.Directory.CreateDirectory("d:\\MyCorp Inc."); 

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

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

尝试:

 using System.Runtime.InteropServices; class Program { [DllImport("kernel32.dll", SetLastError = true)] public static extern bool CreateDirectory(string lpPathName, IntPtr lpSecurityAttributes); private static void Main(string[] args) { CreateDirectory(@"\\?\c:\temp\MyCorp Inc.", IntPtr.Zero); } 

并参考您无法删除NTFS文件系统卷上的文件或文件夹

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

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

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

 c:\drop>mkdir test. c:\drop>dir Volume in drive C has no label. Volume Serial Number is C0F3-338B Directory of c:\drop 2013-04-02 17:54  . 2013-04-02 17:54  .. 2013-04-02 17:54  test 

有关命名reccomendations,请参阅MSDN 。 报价:

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

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

 c:\drop>mkdir \\?\c:\drop\test2. c:\drop>dir Volume in drive C has no label. Volume Serial Number is C0F3-338B Directory of c:\drop 2013-04-02 17:58  . 2013-04-02 17:58  .. 2013-04-02 17:54  test 2013-04-02 17:58  test2. 

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

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

或者在.NET中:

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

更新:

我注意到了两件事!

1)您的要求:

我有很多客户根据公司名称命名他们的文件夹,例如“Some Corp Ltd.”,这些名称通常包含尾随点。 我不能在其他任何地方存储名称,我没有数据库,我无法创建额外的文件,这是要求。

2)你还没有得到答案

我的建议是在目录名的末尾添加一个额外的字符,它将隐藏到普通视图,但仍然存在! 这让我们开心!

试试这个

您必须在目录名称的末尾附加一个特殊字符

 System.IO.Directory.CreateDirectory (@"D:\Sync.inc." + (char)(128)); 

注意:(char)(128)到(char)(158)可能有所帮助

试试爆炸!

 for (int i = 128; i <= 158; i++) { System.IO.Directory.CreateDirectory (@"D:\Sync.inc." + (char)(i)); } 

无论如何,从MS:

如果点位于名称的末尾,Windows将默认忽略点。

看看http://social.technet.microsoft.com/Forums/en-US/w7itproui/thread/84978c71-8203-404b-94cf-b05b14be99db/

如果您尝试在Windows资源管理器中手动执行此操作,即使资源管理器不允许使用尾随点并将其删除。

我想这是Windows本身的一个限制,因为最后一段时间通常表示文件扩展名。

这时我会说这是不可能的。