简体   繁体   English

文件太长异常

[英]File too long exception

I am trying to delete file from remote computer like this: 我试图从远程计算机中删除文件,如下所示:

fileinfo undelteablefile = new fileinfo(
 @"\\CompName\c$\documents and settings\UserName\local settings\temporary internet files\content.ie5\k9ef0hmj\%d7%a9%d7%a2%a8%20%d7%9c%20%d7%a2%93%d7%9b%d7%95%d7%9f%20%d7%98%d7%9d%a0%d795%d7%9c%d7%d7%95%d7%92%d7%99%20%d7%97%95%d7%93%d7%a9%d7%99 [1] .jpg");

undeleteablefile.delete();

It gave me an exception that the file name or path are too long. 它给了我一个例外,文件名或路径太长。

I tried adding \\\\?\\ but it didnt work... Why is this happening and what to do to solve this? 我尝试添加\\\\?\\但它没有工作......为什么会发生这种情况以及如何解决这个问题呢?

Map a network share to one of the directories closer to the file. 将网络共享映射到更靠近文件的目录之一。 Try deleting it with a wildcard. 尝试使用通配符删除它。

There are three approaches that I have used, but be warned that they require unmanaged P/Invoke code. 我使用了三种方法,但要注意它们需要非托管P / Invoke代码。 Not much, so you can probably copy/paste if you don't understand how it works. 不多,所以你可以复制/粘贴,如果你不明白它是如何工作的。

  1. Use the 8.3 filenames (ala DOS compatible filenames) 使用8.3文件名 (ala DOS兼容文件名)

  2. Use the \\?\\ syntax that you mention. 使用您提到的\\?\\语法。 However, I don't believe that you can use it directly from C# - you will need to invoke the native methods. 但是,我不相信你可以直接从C#中使用它 - 你需要调用本机方法。 Note - this has security implications, as the string is not parsed, but passed directly to the filesystem. 注意 - 这具有安全隐患,因为字符串未被解析,而是直接传递给文件系统。

  3. Use the unmanaged File APIs. 使用非托管文件API。

This is a great series of blog posts that address exactly this issue . 这是一系列精彩的博客文章,正是针对这个问题

Erick 埃里克

The long path prefix \\\\?\\ can only be used with non UNC paths. 长路径前缀\\\\?\\只能与非UNC路径一起使用。 Use the \\\\?\\UNC\\ pefix for UNC paths instead and call the Win32 API function DeleteFile . 改为使用UNC路径的\\\\?\\UNC\\ pefix并调用Win32 API函数DeleteFile See the following MSDN article for more information about naming files and paths. 有关命名文件和路径的详细信息,请参阅以下MSDN文章。

Here is a small example: 这是一个小例子:

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool DeleteFile(string lpFileName);

static void Main(string[] args)
{
  string yourLongUncPath = @"\\?\UNC\server\path";

  if(DeleteFile(yourLongUncPath))
  {
    Console.Out.WriteLine("Successfully deleted file...");
  }
}

Please note, that you have to use the Unicode version of DeleteFile to use the long path prefix \\\\?\\UNC\\ . 请注意,您必须使用Unicode版本的DeleteFile才能使用长路径前缀\\\\?\\UNC\\

Looking at the end of the file-name it seems that Windows is not able to handle it due to obscure characters in the name. 查看文件名的末尾,由于名称中的字符不明显,Windows似乎无法处理它。

I used the online facility to decode the file name part, which could theoretically explain why its not able to delete it. 我使用在线工具解码文件名部分,这理论上可以解释为什么它无法删除它。

%d7%a9%d7%a2%a8%20%d7%9c%20%d7%a2%93%d7%9b%d7%95%d7%9f%20%d7%98%d7%9d%a0%d795%d7%9c%d7%d7%95%d7%92%d7%99%20%d7%97%95%d7%93%d7%a9%d7%99 1 .jpg %D7%A9%D7%A2%A8%20%D7%9C%20%D7%A2%93%D7%9B%D7%95%D7%9F%20%D7%98%D7%9D%A0%D795 %d7%9c%d7%d7%95%d7%92%d7%99%20%d7%97%95%d7%93%d7%a9%d7%99 1 .jpg

When converted it looks like this, with US-ASCII 转换时看起来像这样,使用US-ASCII

95 1 .jpg 95 1 .JPG

Likewise for UTF-8 同样适用于UTF-8

שע ל ע כון טם 95ל וגי ח דשי 1 .jpg שע לע כוןטם 95ל וגיח דשי 1 .jpg

Have you not considered that you may have to url-decode it to make it more safer and thereby reducing the length of the filename itself. 您是否认为可能需要对其进行url-decode以使其更安全,从而减少文件名本身的长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM