2012-10-05 20 views
4

我具有複製部分夾260+字符(例如麻煩:F:\ NNNNNNNNNNNNNNNN \ NNNNNNNNNNN \ ROOT \ $ RECYCLE.BIN \ S-1-5-21-3299053755-4209892151-505108915-1000 \ $ RMSL3U8 \ NNNNNNNNN NNNNNNNN \ NNNNNNNNNNN \ NNNNNNNNNN \ NNNNNNNNNN \發佈\應用程序文件\ TNNNNNNNNNNNNN_N0_0_0 \ NNNNNNNNNNNN.exe.manifest)的一些其他地方與standart DrectoryInfo.Create();添加\?\或\?\ UNC \(如「\\?\ UNC \」)只會引發另一個ArgumentException。 我在做什麼錯?還有什麼我可以做,而不使用Directory.SetCurrentDirectory()?PathTooLongException C#4.5

+0

[如何避免System.IO.PathTooLongException?](http://stackoverflow.com/questions/530109/how-to-avoid-system-io-pathtoolongexception) – Deantwo

回答

3

是的,使用標準的API會給你這種限制(255字符IIRC)。

從.NET中,您可以使用允許使用非常長的路徑(使用「\\?\」樣式)並模仿System.IO名稱空間的AlphaFS project

您可能會像使用System.IO一樣使用此庫,例如:AlphaFS.Win32.Filesystem。 File.Copy()改爲System.IO。 File.Copy()

如果你不想或者不能使用AlphaFS你必須PInvoke的Win32 API的

+0

英語和經驗問題的可能的複製。什麼是「必須****製造Win32 **** API」? –

+1

我的意思是 「調用本機的Windows API的C/C++功能」 使用一種叫做的P/Invoke的.NET語言。 – mbarthelemy

+0

噢,好吧,謝謝( –

7

其實你需要從C#調用Win32。我們已經做到了這一點

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

public static class LongPath 
{ 
    static class Win32Native 
    { 
     [StructLayout(LayoutKind.Sequential)] 
     public class SECURITY_ATTRIBUTES 
     { 
      public int nLength; 
      public IntPtr pSecurityDescriptor; 
      public int bInheritHandle; 
     } 

     [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     public static extern bool CreateDirectory(string lpPathName, SECURITY_ATTRIBUTES lpSecurityAttributes); 

     [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     public static extern SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES securityAttrs, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile); 
    } 

    public static bool CreateDirectory(string path) 
    { 
     return Win32Native.CreateDirectory(String.Concat(@"\\?\", path), null); 
    } 

    public static FileStream Open(string path, FileMode mode, FileAccess access) 
    { 
     SafeFileHandle handle = Win32Native.CreateFile(String.Concat(@"\\?\", path), (int)0x10000000, FileShare.None, null, mode, (int)0x00000080, IntPtr.Zero); 
     if (handle.IsInvalid) 
     { 
      throw new System.ComponentModel.Win32Exception(); 
     } 
     return new FileStream(handle, access); 
    } 
} 

示例代碼:

string path = @"c:\".PadRight(255, 'a'); 
LongPath.CreateDirectory(path); 

path = String.Concat(path, @"\", "".PadRight(255, 'a')); 
LongPath.CreateDirectory(path); 

string filename = Path.Combine(path, "test.txt"); 

FileStream fs = LongPath.Open(filename, FileMode.CreateNew, FileAccess.Write); 

using (StreamWriter sw = new StreamWriter(fs)) 
{ 
    sw.WriteLine("abc"); 
} 
+0

是的,這是很大的一個了。 –

7

有克服長文件名的問題Microsoft TechNet上一個偉大的圖書館,這就是所謂的Delimon.Win32.I​O Library (V4.0),它有其自身的主要方法版本從System.IO

例如,你將取代:

System.IO.Directory.GetFiles 

Delimon.Win32.IO.Directory.GetFiles 

這將讓你處理長文件和文件夾。

從網站:

Delimon.Win32.IO取代System.IO和 支持的基本文件功能文件&文件夾名最多可達32,767個字符。

此庫是在.NET Framework 4.0上編寫的,可以在x86 & x64系統上使用 。標準 System.IO命名空間的文件夾&限制可以用在一個 文件名260個字符和240個字符的文件夾名稱文件的工作(MAX_PATH通常是 配置爲260個字符)。通常,您會遇到標準.NET庫的 System.IO.PathTooLongException錯誤。

+0

Delimon是偉大的,我已經使用了一段時間,現在,但它不支持的FileStream。作爲Vinoth說,最好從C#調用Win32。 –