2010-08-09 2 views
2

Windows 서버의 Cygwin을 통해 rsync에서 만든 마침표 (.)로 끝나는 디렉토리가 있습니다.C# - 기간이 끝나는 디렉토리 읽기.

코드에서이 디렉터리 내의 파일을 읽으려고했지만 "경로의 일부를 찾을 수 없습니다"라는 예외가 발생하고 프로그램에서 읽으려고하는 경로에서 끝나는 기간이 누락되었습니다.

C#을 통해 마침표로 끝나는 디렉토리를 읽을 수 있습니까?

도움 주셔서 감사합니다.

코드 사용에 beeing :

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

은 우리에게 당신이 curently 사용하는 코드를 보여하세요? –

+0

기간이 끝나는 Windows Vista에서 디렉토리를 만들 수 없습니다. 나는 Exporer와 명령 프롬프트를 시도했다. 두 방법 모두 디렉토리의 이름을 만들지 만 마침표는 생략합니다. 편집 : 같은 파일을 간다. –

답변

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

+0

매핑 된 드라이브와 바이너리가 위치한 드라이브 이외의 다른 드라이브에서 작동하는지 알고 있습니까? – Freddy

+0

코드 샘플을 다른 방법으로 변경했습니다. –

2

은 DOS/Windows의 경우 기간은 연장 구분하고, 심지어 디렉토리 이름은 확장자를 가지고있다.

따라서 경로 "c:\some\path.""c:\some\path"과 같습니다. 마침표가있는 디렉토리에 액세스하려고하면 디렉토리가없는 디렉토리에 실제로 액세스하게됩니다. 따라서 마침표가있는 마침표가있는 디렉토리 이름을 작성했다면 사용할 수 없습니다.

관련 문제