Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 977
yosi roz Avatar asked May 26 '26 13:05

yosi roz


2 Answers

Map a network share to one of the directories closer to the file. Try deleting it with a wildcard.

like image 110
Chriseyre2000 Avatar answered May 28 '26 03:05

Chriseyre2000


There are three approaches that I have used, but be warned that they require unmanaged P/Invoke code. 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)

  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. Note - this has security implications, as the string is not parsed, but passed directly to the filesystem.

  3. Use the unmanaged File APIs.

This is a great series of blog posts that address exactly this issue.

Erick

like image 39
Erick T Avatar answered May 28 '26 02:05

Erick T



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!