2010-08-09 56 views
2

我有一個目錄以一個句點(。)結尾,由rsync通過Cygwin在Windows服務器上創建。C# - 閱讀目錄結尾的時期

我試圖讀取此目錄中的文件的代碼,但我得到一個異常,說它「無法找到路徑的一部分」,並且程序試圖讀取的路徑中缺少結束週期。

是否有可能通過C#讀取以句點結尾的目錄?

謝謝你的幫助。

代碼beeing使用:

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

請告訴我們,你是curently使用的代碼? – 2010-08-09 12:21:48

+0

我無法在Windows Vista上創建以一段時間結束的目錄。我嘗試通過Exporer和一個命令提示符。兩種方法都會創建目錄的名稱,但省略句號。編輯:一樣的文件。 – 2010-08-09 12:30:22

回答

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); 
    } 
} 
+0

謝謝Mikael,我會試試這個。 – Freddy 2010-08-09 13:33:47

+0

您是否知道這是否適用於映射驅動器和驅動器,而不是二進制文件所在的位置? – Freddy 2010-08-09 14:53:59

+0

我將我的代碼示例更改爲其他方法。 – 2010-08-09 19:53:45

2

在DOS/Windows的時期是延長的隔板,甚至是目錄名有擴展。所以路徑"c:\some\path.""c:\some\path"相同。如果您嘗試訪問具有句點的目錄,它將實際訪問它,因此如果您設法創建了一個在句尾處有句號的目錄名稱,則無法使用它。