需要使用win32 api来复制c#中的文件 - 我该怎么做?

时间:2020-12-22 07:04:47

I'm trying to copy some files around, and occasionally the lengths of the names exceeds the length that the System.IO.File.Copy method can accept (260 chars according to the exception that is getting thrown)

我正在尝试复制一些文件,偶尔名称的长度超过System.IO.File.Copy方法可以接受的长度(260个字符根据被抛出的异常)

According to the research I've done, I should be able to use the win32 api's file methods in conjunction with \?\ prepended to paths to get a 32,000 character limit, but I'm not sure witch methods I need to import.

根据我所做的研究,我应该能够将win32 api的文件方法与\?\ prepended to path一起使用以获得32,000个字符的限制,但我不确定我需要导入的方法。

Can someone help me with this? I'm looking for something like (obviously a different function, but you get the idea):

有人可以帮我弄这个吗?我正在寻找类似的东西(显然是一个不同的功能,但你明白了):

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    static extern SafeFileHandle CreateFileW(string lpFileName, uint dwDesiredAccess,
                                          uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,
                                          uint dwFlagsAndAttributes, IntPtr hTemplateFile);

2 个解决方案

#1


[DllImport("kernel32.dll",
           CharSet = CharSet.Unicode,
           CallingConvention = CallingConvention.StdCall,
           SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool CopyFile(
                   [MarshalAs(UnmanagedType.LPWStr)] string lpExistingFileName,
                   [MarshalAs(UnmanagedType.LPWStr)] string lpNewFileName,
                   [MarshalAs(UnmanagedType.Bool)] bool bFailIfExists);

http://www.pinvoke.net/default.aspx/kernel32/CopyFile.html

#2


Try using CopyFile.

尝试使用CopyFile。

For PInvoke syntax, you can check pinvoke.net.

对于PInvoke语法,您可以检查pinvoke.net。

#1


[DllImport("kernel32.dll",
           CharSet = CharSet.Unicode,
           CallingConvention = CallingConvention.StdCall,
           SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool CopyFile(
                   [MarshalAs(UnmanagedType.LPWStr)] string lpExistingFileName,
                   [MarshalAs(UnmanagedType.LPWStr)] string lpNewFileName,
                   [MarshalAs(UnmanagedType.Bool)] bool bFailIfExists);

http://www.pinvoke.net/default.aspx/kernel32/CopyFile.html

#2


Try using CopyFile.

尝试使用CopyFile。

For PInvoke syntax, you can check pinvoke.net.

对于PInvoke语法,您可以检查pinvoke.net。