C# - 读取以句点结尾的目录

时间:2010-08-09 12:19:47

标签: c#

我有一个以句点(。)结尾的目录,由rsync通过Cygwin在Windows服务器上创建。

我尝试读取此目录中的文件的代码,但我得到一个例外,说它“无法找到路径的一部分”,并且程序尝试读取的路径中缺少结束时间。

是否可以通过C#读取以句点结尾的目录?

感谢您的帮助。

使用的代码:

StreamReader sr = null;
try
  {
    sr = new StreamReader(@"<path_ending_in_period>", System.Text.Encoding.Default);
  }
catch (Exception ex)
  {
            ....
  }

2 个答案:

答案 0 :(得分:2)

在DOS / Windows中,句点是扩展名的分隔符,甚至目录名也有扩展名。

因此,路径"c:\some\path.""c:\some\path"相同。如果您尝试访问带有句点的目录,它实际上将在没有它的情况下访问它,因此如果您设法在结尾处创建一个句点名称,则无法使用它。

答案 1 :(得分:0)

检查下面的Test()方法。它包含本地和UNC路径的示例,并使用以点结尾的文件。该代码基于http://blogs.msdn.com/b/bclteam/archive/2007/03/26/long-paths-in-net-part-2-of-3-long-path-workarounds-kim-hamilton.aspx处的代码,该代码还包含用于删除文件的代码。

基本上你从Win32 API获得一个FileHandle,并将其传递给.Net。

[编辑 - 新密码]

using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;

namespace ConsoleApplication1
{
    internal class WeirdFilename
    {
        public static void Test()
        {
            //string formattedName = @"\\?\c:\temp\dot.";
            string formattedName = @"\\?\UNC\m1330\c$\temp\dot.";
            SafeFileHandle fileHandle = CreateFile(formattedName,
                                                    EFileAccess.GenericRead, EFileShare.None, IntPtr.Zero,
                                                    ECreationDisposition.OpenExisting, 0, IntPtr.Zero);

            // Check for errors
            int lastWin32Error = Marshal.GetLastWin32Error();
            if (fileHandle.IsInvalid)
            {
                throw new Win32Exception(lastWin32Error);
            }

            // Pass the file handle to FileStream. FileStream will close the handle
            using (FileStream fs = new FileStream(fileHandle, FileAccess.Read))
            {
                StreamReader reader = new StreamReader(fs);
            }
        }


        #region ECreationDisposition enum

        public enum ECreationDisposition : uint
        {
            New = 1,
            CreateAlways = 2,
            OpenExisting = 3,
            OpenAlways = 4,
            TruncateExisting = 5,
        }

        #endregion

        #region EFileAccess enum

        [Flags]
        public enum EFileAccess : uint
        {
            GenericRead = 0x80000000,
            GenericWrite = 0x40000000,
            GenericExecute = 0x20000000,
            GenericAll = 0x10000000,
        }

        #endregion

        #region EFileAttributes enum

        [Flags]
        public enum EFileAttributes : uint
        {
            Readonly = 0x00000001,
            Hidden = 0x00000002,
            System = 0x00000004,
            Directory = 0x00000010,
            Archive = 0x00000020,
            Device = 0x00000040,
            Normal = 0x00000080,
            Temporary = 0x00000100,
            SparseFile = 0x00000200,
            ReparsePoint = 0x00000400,
            Compressed = 0x00000800,
            Offline = 0x00001000,
            NotContentIndexed = 0x00002000,
            Encrypted = 0x00004000,
            Write_Through = 0x80000000,
            Overlapped = 0x40000000,
            NoBuffering = 0x20000000,
            RandomAccess = 0x10000000,
            SequentialScan = 0x08000000,
            DeleteOnClose = 0x04000000,
            BackupSemantics = 0x02000000,
            PosixSemantics = 0x01000000,
            OpenReparsePoint = 0x00200000,
            OpenNoRecall = 0x00100000,
            FirstPipeInstance = 0x00080000
        }

        #endregion

        #region EFileShare enum

        [Flags]
        public enum EFileShare : uint
        {
            None = 0x00000000,
            Read = 0x00000001,
            Write = 0x00000002,
            Delete = 0x00000004,
        }

        #endregion

        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        internal static extern SafeFileHandle CreateFile(
            string lpFileName,
            EFileAccess dwDesiredAccess,
            EFileShare dwShareMode,
            IntPtr lpSecurityAttributes,
            ECreationDisposition dwCreationDisposition,
            EFileAttributes dwFlagsAndAttributes,
            IntPtr hTemplateFile);
    }
}