private static readonly string _fileDataStreamFormat="{0}:{1}";
public static byte[] GetAlternateDataStream(string fileName, string dataStreamName)
{
if(dataStreamName.Trim().Length==0)
throw new ArgumentException("dataStreamName must not be an empty string", "dataStreamName");
string filePath = Path.GetFullPath(fileName);
string adsPath = string.Format(_fileDataStreamFormat, filePath, dataStreamName);
if(!File.Exists(filePath))
throw new FileNotFoundException(string.Format("The file path '{0}' cannot be found.
Please check the file location and provide full path info", filePath));
IntPtr hFile = MemoryMappedFileHelper.CreateFile(adsPath
, Win32FileAccess.GENERIC_READ
, Win32FileShare.FILE_SHARE_READ
, IntPtr.Zero
, Win32FileMode.OPEN_ALWAYS
, 0
, IntPtr.Zero);
FileStream stream = new FileStream(hFile, FileAccess.Read);
int length = Convert.ToInt32(stream.Length);
byte[] streamValues = new byte[length];
try
{
int totalRead=0;
while(totalRead<length)
{
totalRead+=stream.Read(streamValues, totalRead, length-totalRead);
}
}
finally
{
stream.Close();
}
return streamValues;
}