Ejemplo n.º 1
0
        //private async Task<XDocument> LoadAsync(string path)
        //{
        //    StorageFolder sf = await ApplicationData.Current.LocalFolder.GetFolderAsync(@"data\");
        //    StorageFile file = await sf.GetFileAsync(Path.GetFileName(path));
        //    IBuffer ibuf = null;
        //    byte[] b;
        //    bool excCaught = false;
        //    try
        //    {
        //        using (IRandomAccessStream ras = await file.OpenReadAsync())
        //        {
        //            b = new byte[ras.Size];
        //            ibuf = await ras.ReadAsync(CryptographicBuffer.CreateFromByteArray(b), (uint)ras.Size, InputStreamOptions.None);
        //            ras.Dispose();
        //        }
        //    }
        //    catch (System.UnauthorizedAccessException ex)
        //    {
        //        excCaught = true;
        //    }
        //    if (excCaught)
        //    {
        //        using (IRandomAccessStream ras = await file.OpenReadAsync())
        //        {
        //            b = new byte[ras.Size];
        //            ibuf = await ras.ReadAsync(CryptographicBuffer.CreateFromByteArray(b), (uint)ras.Size, InputStreamOptions.None);
        //            ras.Dispose();
        //        }
        //    }
        //    CryptographicBuffer.CopyToByteArray(ibuf, out b);
        //    char[] ch = System.Text.Encoding.UTF8.GetChars(b);
        //    string s = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, ibuf);
        //    s = EncryptionProvider.Decrypt(s, EncryptionProvider.PublicKey);
        //    StringReader sreader = new StringReader(s);
        //    while (sreader.Peek() == 65279)
        //    {
        //        sreader.Read();
        //    }
        //    XDocument xdoc = XDocument.Load(sreader, LoadOptions.None);

        //    return xdoc;

        //}

        private async Task <XDocument> LoadAsync(string path)
        {
            StorageFolder sf = await ApplicationData.Current.LocalFolder.GetFolderAsync(@"data\");

            StorageFile file = await sf.GetFileAsync(Path.GetFileName(path));

            IBuffer ibuf = null;

            byte[] b;
            using (IRandomAccessStream ras = await file.OpenReadAsync())
            {
                using (IInputStream inputStream = ras.GetInputStreamAt(0))
                {
                    ulong      size       = ras.Size;
                    DataReader dataReader = new DataReader(inputStream);
                    await dataReader.LoadAsync((uint)size);

                    ibuf = dataReader.ReadBuffer((uint)size);
                    inputStream.Dispose();
                }
                ras.Dispose();
            }
            CryptographicBuffer.CopyToByteArray(ibuf, out b);
            // char[] ch = System.Text.Encoding.UTF8.GetChars(b);
            // string s = CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, ibuf);
            string       s       = EncryptionProvider.DecryptToString(ibuf);
            StringReader sreader = new StringReader(s);

            while (sreader.Peek() == 65279)
            {
                sreader.Read();
            }
            XDocument xdoc = XDocument.Load(sreader, LoadOptions.None);

            return(xdoc);
        }