StreamReader file = new StreamReader("D:\\Tmp\\Alternate.XML:Zone.Identifier");gives exception:
Class : PlatformFileStreams // not really needed for this purpose public static List<PlatformStreamInfo> GetStreams(string fileName) returns a list of the streams public static bool HasSecurityStream(string path) tells whether a file has a security stream attached to it public static bool DeleteSecurityStream(string path) deletes the security stream (restores the file readonly flag when necessary
Class FileBits collection of methods on files
// test namespace GregoryAdam.Test { class testx { static void Main(string[] args) { ShowStreams(@"d:\tmp\in\Adam9.exe"); Console.WriteLine(""); Console.WriteLine(" {0}", PlatformFileStreams.HasSecurityStream(@"d:\tmp\in\Adam9.exe")); Console.WriteLine(" {0}", PlatformFileStreams.DeleteSecurityStream(@"d:\tmp\in\Adam9.exe")); Console.WriteLine(" {0}", PlatformFileStreams.HasSecurityStream(@"d:\tmp\in\Adam9.exe")); Console.ReadLine(); } static void ShowStreams(string fileName) { Console.WriteLine(" >>> {0}", fileName); var si = PlatformFileStreams.GetStreams(fileName); foreach (PlatformStreamInfo xx in si) { Console.WriteLine(" {0} {1} {2}", xx.Name == null ? "(no name)" : xx.Name , xx.Type, xx.Size); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace GregoryAdam.Base.IO { public static class FileBits { //---------------------------------------------------------------------- public static bool ReadOnly(string path) { if (!File.Exists(path)) return false; return ( (File.GetAttributes(path) & FileAttributes.ReadOnly) != 0); } //---------------------------------------------------------------------- public static bool Hidden(string path) { if (!File.Exists(path)) return false; return ((File.GetAttributes(path) & FileAttributes.Hidden) != 0); } //---------------------------------------------------------------------- //---------------------------------------------------------------------- public static bool ResetReadOnly(string path) { if (!FileBits.ReadOnly(path)) return false; try { File.SetAttributes(path, File.GetAttributes(path) & ~FileAttributes.ReadOnly); return true; } catch { return false; } } //---------------------------------------------------------------------- public static bool SetReadOnly(string path) { if (FileBits.ReadOnly(path)) return false; try { File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.ReadOnly); return true; } catch { return false; } } //---------------------------------------------------------------------- } }
// a big ugly program using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Security.Permissions; using Microsoft.Win32.SafeHandles; using System.Runtime.ConstrainedExecution; using System.IO; namespace GregoryAdam.Base.IO { [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct Win32StreamID { public PlatformStreamType dwStreamId; public int dwStreamAttributes; public long Size; public int dwStreamNameSize; // WCHAR cStreamName[1]; } public enum PlatformStreamType { Data = 1, ExternalData = 2, SecurityData = 3, AlternateData = 4, Link = 5, PropertyData = 6, ObjectID = 7, ReparseData = 8, SparseDock = 9 } public struct PlatformStreamInfo { public PlatformStreamInfo(string name, PlatformStreamType type, long size) { Name = name; Type = type; Size = size; } public readonly string Name; public readonly PlatformStreamType Type; public readonly long Size; } public static class PlatformFileStreams { public const string PlatformStreamSecurityStreamName = @":Zone.Identifier:$DATA"; [DllImport("kernel32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool BackupRead( SafeFileHandle hFile, IntPtr lpBuffer, uint nNumberOfBytesToRead, out uint lpNumberOfBytesRead, [MarshalAs(UnmanagedType.Bool)] bool bAbort, [MarshalAs(UnmanagedType.Bool)] bool bProcessSecurity, ref IntPtr lpContext ); [DllImport("kernel32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool BackupSeek( SafeFileHandle hFile, uint dwLowBytesToSeek, uint dwHighBytesToSeek, out uint lpdwLowByteSeeked, out uint lpdwHighByteSeeked, ref IntPtr lpContext ); /// <summary> /// returns a List of PlatformStreamInfo for a fileName, null if any error /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static List<PlatformStreamInfo> GetStreams(string fileName) { if (fileName == null) return null; FileInfo fileInfo = null; try { fileInfo = new FileInfo(fileName); } catch { return null ; } var streamList = new List<PlatformStreamInfo>(); const int bufferSize = 4096; IntPtr context = IntPtr.Zero; IntPtr buffer = Marshal.AllocHGlobal(bufferSize); uint numRead; Win32StreamID streamID; string name; using (FileStream fs = fileInfo.OpenRead()) { try { while (true) { if (!BackupRead(fs.SafeFileHandle, buffer, (uint)Marshal.SizeOf(typeof(Win32StreamID)), out numRead, false, true, ref context ) ) return null; if (numRead <= 0) break; streamID = (Win32StreamID) Marshal.PtrToStructure(buffer, typeof(Win32StreamID)); name = null; if (streamID.dwStreamNameSize > 0) { if (!BackupRead(fs.SafeFileHandle, buffer, (uint)Math.Min(bufferSize, streamID.dwStreamNameSize), out numRead, false, true, ref context ) ) return null; name = Marshal.PtrToStringUni(buffer, (int)numRead / 2); } streamList.Add(new PlatformStreamInfo(name, streamID.dwStreamId, streamID.Size)); if (streamID.Size > 0) { uint lo, hi; BackupSeek( fs.SafeFileHandle, uint.MaxValue, int.MaxValue, out lo, out hi, ref context ); } } } finally { Marshal.FreeHGlobal(buffer); BackupRead( fs.SafeFileHandle, IntPtr.Zero, 0, out numRead, true, false, ref context ); } } //using return streamList; } //---------------------------------------------------------------------- [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern int GetFileAttributes(string lpFileName); public static bool HasSecurityStream(string path) { return GetFileAttributes(path + PlatformStreamSecurityStreamName) != -1; } //---------------------------------------------------------------------- [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern bool DeleteFile(string lpFileName); public static bool DeleteSecurityStream(string path) { if (!PlatformFileStreams.HasSecurityStream(path)) return false; if( !FileBits.ReadOnly(path) ) return DeleteFile(path + PlatformStreamSecurityStreamName) ; if (!FileBits.ResetReadOnly(path)) return false; if (!DeleteFile(path + PlatformStreamSecurityStreamName)) return false; if (!FileBits.SetReadOnly(path)) return false; return true; } } }