1.文件选择
var openPicker = new FileOpenPicker
{
ViewMode = PickerViewMode.Thumbnail,
SuggestedStartLocation = PickerLocationId.ComputerFolder
};
openPicker.FileTypeFilter.Add("*");
StorageFile file = await openPicker.PickSingleFileAsync();
{
ViewMode = PickerViewMode.Thumbnail,
SuggestedStartLocation = PickerLocationId.ComputerFolder
};
openPicker.FileTypeFilter.Add("*");
StorageFile file = await openPicker.PickSingleFileAsync();
2.获取文件大小
BasicProperties pro = await file.GetBasicPropertiesAsync();
UInt64 size = pro.Size;
UInt64 size = pro.Size;
3.convert StorageFile to byte[]
- var bytes = await GetBtyeFromFile(file);
- // This is the method to convert the StorageFile to a Byte[]
- private async Task GetBtyeFromFile(StorageFile storageFile)
- {
- var stream = await storageFile.OpenReadAsync();
- using (var dataReader = new DataReader(stream))
- {
- var bytes = new byte[stream.Size];
- await dataReader.LoadAsync((uint)stream.Size);
- dataReader.ReadBytes(bytes);
- return bytes;
- }
- }
4.convert the Byte[] to an Image
- var stream = new MemoryStream(bytes);
- var randomAccessStream = new MemoryRandomAccessStream(stream);
- var bi = new BitmapImage();
- bi.ImageFailed += (s, o) =>
- {
- // This event is optional and is used to let us know if something went wrong
- var m = "Failure;
- };
- bi.SetSource(randomAccessStream);
private async Task<string> GetMd5(StorageFile file)
{
HashAlgorithmProvider alg = Windows.Security.Cryptography.Core.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
var stream = await file.OpenStreamForReadAsync();
var inputStream = stream.AsInputStream();
const uint capacity = 100000000;
var buffer = new Windows.Storage.Streams.Buffer(capacity);
var hash = alg.CreateHash();
while (true)
{
await inputStream.ReadAsync(buffer, capacity, InputStreamOptions.None);
if (buffer.Length > 0)
hash.Append(buffer);
else
break;
}
string hashText = CryptographicBuffer.EncodeToHexString(hash.GetValueAndReset()).ToUpper();
inputStream.Dispose();
stream.Dispose();
return hashText;
}
{
HashAlgorithmProvider alg = Windows.Security.Cryptography.Core.HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
var stream = await file.OpenStreamForReadAsync();
var inputStream = stream.AsInputStream();
const uint capacity = 100000000;
var buffer = new Windows.Storage.Streams.Buffer(capacity);
var hash = alg.CreateHash();
while (true)
{
await inputStream.ReadAsync(buffer, capacity, InputStreamOptions.None);
if (buffer.Length > 0)
hash.Append(buffer);
else
break;
}
string hashText = CryptographicBuffer.EncodeToHexString(hash.GetValueAndReset()).ToUpper();
inputStream.Dispose();
stream.Dispose();
return hashText;
}
6. 生成GUID
1、Guid.NewGuid().ToString("N") 结果为:
38bddf48f43c48588e0d78761eaa1ce6
2、Guid.NewGuid().ToString("D") 结果为:
57d99d89-caab-482a-a0e9-a0a803eed3ba
3、Guid.NewGuid().ToString("B") 结果为:
{09f140d5-af72-44ba-a763-c861304b46f8}
4、Guid.NewGuid().ToString("P") 结果为:
(778406c2-efff-4262-ab03-70a77d09c2b5)
默认第2种效果