Исходный список можно посмотреть здесь: http://support.microsoft.com/kb/320081
Интерес представляют 4 и 6, поскольку в последнее время часто приходилось сталкиваться с этим.
Такой файл средствами .NET невозможно ни удалить, ни переименовать.
Вкратце одно из возможных решений:
1. Преобразовать файл к короткому имени.
2. Выполнить копирование файла в новое имя при помощи CopyFileEx
* This source code was highlighted with Source Code Highlighter.
- [Flags]
- public enum CopyFileFlags : uint
- {
- COPY_FILE_FAIL_IF_EXISTS = 0x00000001,
- COPY_FILE_RESTARTABLE = 0x00000002,
- COPY_FILE_OPEN_SOURCE_FOR_WRITE = 0x00000004,
- COPY_FILE_ALLOW_DECRYPTED_DESTINATION = 0x00000008
- }
- public enum CopyProgressCallbackReason : uint
- {
- CALLBACK_CHUNK_FINISHED = 0x00000000,
- CALLBACK_STREAM_SWITCH = 0x00000001
- }
- public enum CopyProgressResult : uint
- {
- PROGRESS_CONTINUE = 0,
- PROGRESS_CANCEL = 1,
- PROGRESS_STOP = 2,
- PROGRESS_QUIET = 3
- }
- public delegate CopyProgressResult CopyProgressRoutine(
- long totalFileSize,
- long totalBytesTransferred,
- long streamSize,
- long streamBytesTransferred,
- uint dwStreamNumber,
- CopyProgressCallbackReason dwCallbackReason,
- IntPtr hSourceFile,
- IntPtr hDestinationFile,
- IntPtr lpData);
- public static bool XCopy(string oldFile, string newFile)
- {
- var pbCancel = 0;
- return CopyFileEx(oldFile, newFile, CopyProgressHandler, IntPtr.Zero, ref pbCancel, CopyFileFlags.COPY_FILE_RESTARTABLE);
- }
- public static CopyProgressResult CopyProgressHandler(long total, long transferred, long streamSize,
- long streamByteTrans, uint dwStreamNumber,
- CopyProgressCallbackReason reason, IntPtr hSourceFile,
- IntPtr hDestinationFile, IntPtr lpData)
- {
- return CopyProgressResult.PROGRESS_CONTINUE;
- }
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
- [return: MarshalAs(UnmanagedType.Bool)]
- public static extern bool CopyFileEx(string lpExistingFileName, string lpNewFileName,
- CopyProgressRoutine lpProgressRoutine, IntPtr lpData, ref Int32 pbCancel,
- CopyFileFlags dwCopyFlags);
- [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
- public static extern int GetShortPathName([MarshalAs(UnmanagedType.LPTStr)] string path,
- [MarshalAs(UnmanagedType.LPTStr)] StringBuilder shortPath,
- int shortPathLength);
- public static string UPath(string path)
- {
- const string PreNet = @"\\?\UNC\";
- const string PreLoc = @"\\?\";
- var isNetworkPath = path.StartsWith(@"\\");
- var pre = isNetworkPath ? PreNet : PreLoc;
- var res = path.StartsWith(pre) ? path : pre + path;
- return res.EndsWith(@"\") ? res.Substring(0, res.Length - 1) : res;
- }
- public static FileInfo XCopy(FileInfo fileInfo, string target)
- {
- var filename = Win32IOManaged.UPath(Path.GetDirectoryName(fileInfo.FullName)) + @"
\" + fileInfo.Name;- var buffer = new StringBuilder(255);
- var length = Win32IOManaged.GetShortPathName(filename, buffer, buffer.Capacity);
- if (length <= 0)
- {
- return null;
- }
- var result = Win32IOManaged.XCopy(buffer.ToString(), target);
- var lasterror = Marshal.GetLastWin32Error();
- if (!result || lasterror != 0)
- {
- return null;
- }
- return new FileInfo(target);
- }
0 коммент.:
Post a Comment