Esempio n. 1
0
        public static async Task <T> DeserializeAsync <T>(Windows.Storage.StorageFolder folder, string fileName, System.Threading.SemaphoreSlim semaphore) where T : class
        {
            await semaphore.WaitAsync();

            try
            {
                if (await folder.TryGetItemAsync(fileName) is Windows.Storage.StorageFile f)
                {
                    using (var s = (await f.OpenAsync(Windows.Storage.FileAccessMode.Read)).AsStream())
                    {
                        var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
                        return((T)serializer.Deserialize(s));
                    }
                }
                else
                {
                    return(null);
                }
            }
            catch
            {
                return(null);
            }
            finally
            {
                semaphore.Release();
            }
        }
Esempio n. 2
0
        private async void ProtectData()
        {
            if (await storageFolder.TryGetItemAsync("config.txt") != null) // if user file exists
            {
                configSF = await storageFolder.GetFileAsync("config.txt");

                var stream = await configSF.OpenAsync(Windows.Storage.FileAccessMode.Read);

                ulong size = stream.Size;
                using (var inputStream = stream.GetInputStreamAt(0))
                {
                    using (var dataReader = new Windows.Storage.Streams.DataReader(inputStream))
                    {
                        uint numBytesLoaded = await dataReader.LoadAsync((uint)size);

                        IBuffer textBuffer = dataReader.ReadBuffer(numBytesLoaded);
                        string  text       = await SampleDataUnprotectStream(textBuffer, encoding);

                        if (text.Length != 0 && text.IndexOf(" ") > 0)
                        {
                            Username.Text     = text.Substring(0, text.IndexOf(" "));
                            Password.Password = text.Substring(text.IndexOf(" ") + 1);
                            saveLogin         = true;
                            SavePasswordCheckBox.IsChecked = true;
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        private async void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            string fileName = this.textFilename.Text;
            string content  = this.textContent.Text;

            Windows.Storage.StorageFolder storageFolder =
                Windows.Storage.ApplicationData.Current.LocalFolder;

            if (await storageFolder.TryGetItemAsync(fileName) == null)
            {
                DisplayNoWifiDialog("Search result", "File not found");
            }
            else
            {
                Windows.Storage.StorageFile choosenFile =
                    await storageFolder.GetFileAsync(fileName);

                string text = await Windows.Storage.FileIO.ReadTextAsync(choosenFile);

                if (text.Contains(content))
                {
                    DisplayNoWifiDialog("Search result", "File found and text found");
                }
                else
                {
                    DisplayNoWifiDialog("Search result", "File found but text not found");
                }
            }
        }
Esempio n. 4
0
        SlamSettings LoadFile()
        {
            SlamSettings sl   = null;
            string       ret  = null;
            Task <Task>  task = Task <Task> .Factory.StartNew(
                async() =>
            {
                try
                {
                    Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
                    //C:\Users\awonn\AppData\Local\Packages\19034vSlam.org.vSlam3DPro_1h1swmnfvff8a\LocalState
                    var f = await storageFolder.TryGetItemAsync(dataFileName);
                    if (f != null)
                    {
                        Windows.Storage.StorageFile sampleFile = await storageFolder.GetFileAsync(dataFileName);
                        ret = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);
                        await Task.Factory.StartNew(() => sl = Newtonsoft.Json.JsonConvert.DeserializeObject <SlamSettings>(ret));
                    }
                }
                catch (Exception x) { }
            });

            task.Wait();
            task.Result.Wait();

            return(sl);
        }
Esempio n. 5
0
        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            string fileName = this.searchFile.Text;

            Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            if (await storageFolder.TryGetItemAsync(fileName) != null)
            {
                Windows.Storage.StorageFile sampleFile = await storageFolder.GetFileAsync(fileName);

                string text = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);

                if (text.Contains(TextSeach.Text))
                {
                    textAblet.Text = "File Found and text found";
                }
                else
                {
                    textAblet.Text = "File Found but text not found";
                }
            }
            else
            {
                textAblet.Text = "File not found ";
            }
        }
Esempio n. 6
0
        public async void loadRecentTracks()
        {
            // load bookmark json from file
            Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

            if (await storageFolder.TryGetItemAsync(recentTracks) != null)
            {
                Windows.Storage.StorageFile storage = await storageFolder.GetFileAsync(recentTracks);

                string json = await Windows.Storage.FileIO.ReadTextAsync(storage);

                recentlyPlayed = JsonConvert.DeserializeObject <List <string> >(json);

                foreach (var track in recentlyPlayed)
                {
                    Windows.Storage.StorageFile file = await Windows.Storage.StorageFile.GetFileFromPathAsync(track);

                    if (file != null)
                    {
                        // Application now has read/write access to the picked file
                        var t = new Track(file);
                        t.Tapped += track_Clicked;
                        trackList.Items.Add(t);
                    }
                }
            }
            else
            {
                recentlyPlayed = new List <string>();
            }
        }
Esempio n. 7
0
 private async void InnerFrame_Navigated(object sender, NavigationEventArgs e)
 {
     if (await storageFolder.TryGetItemAsync("config.txt") != null)
     {
         // Login with settings, navigate to different page
     }
     else
     {
         // Do nothing?
     }
 }
Esempio n. 8
0
        /// <summary>
        /// Læser værdier fra fil, returnerer værdier som string.
        /// </summary>
        /// <param name="fileName">Navnet på den lokale fil</param>
        /// <returns></returns>
        public static async Task <string> DeSerializeBoligFileAsync(String fileName)
        {
            string fileString = null;

            Windows.Storage.StorageFolder myStorageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

            if (await myStorageFolder.TryGetItemAsync(fileName) != null)
            {
                Windows.Storage.StorageFile myStorageFile = await myStorageFolder.GetFileAsync(fileName);

                fileString = await Windows.Storage.FileIO.ReadTextAsync(myStorageFile);
            }
            return(fileString);
        }
Esempio n. 9
0
        public async void loadBookmarkCollection()
        {
            // load bookmark json from file
            Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

            if (await storageFolder.TryGetItemAsync(storageFile) != null)
            {
                Windows.Storage.StorageFile storage = await storageFolder.GetFileAsync(storageFile);

                string json = await Windows.Storage.FileIO.ReadTextAsync(storage);

                // deserialize it
                bookmarkStorage = JsonConvert.DeserializeObject <List <BookmarkCollection> >(json);
            }
            else
            {
                bookmarkStorage = new List <BookmarkCollection>();
            }
        }
Esempio n. 10
0
        private async void BtnLoad_Click(object sender, RoutedEventArgs e)
        {
            Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            const string FILE_NAME = "save.txt";

            if (await storageFolder.TryGetItemAsync(FILE_NAME) != null)
            {
                Windows.Storage.StorageFile storageFile =
                    await storageFolder.GetFileAsync(FILE_NAME);

                txtBoxSave.Text = await Windows.Storage.FileIO.ReadTextAsync(storageFile);

                txtLoadResult.Text = "불러오기 완료!";
            }
            else
            {
                txtLoadResult.Text = "불러오기 실패: 파일이 존재하지 않습니다.";
            }
            FlyoutBase.ShowAttachedFlyout((FrameworkElement)sender);
        }
Esempio n. 11
0
        public async void saveRecentTracks()
        {
            // Serialize collection
            string json = JsonConvert.SerializeObject(recentlyPlayed);

            // save collection to file
            Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            Windows.Storage.StorageFile   storage       = null;

            if (await storageFolder.TryGetItemAsync(recentTracks) == null)
            {
                storage = await storageFolder.CreateFileAsync(recentTracks);
            }
            else
            {
                storage = await storageFolder.GetFileAsync(recentTracks);
            }

            await Windows.Storage.FileIO.WriteTextAsync(storage, json);
        }
        public static async Task <List <MasterDictionary> > Load(string FileName)
        {
            List <MasterDictionary> DataList = new List <MasterDictionary>();

            Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

            var file = await storageFolder.TryGetItemAsync(FileName);

            if (file != null)
            {
                Windows.Storage.FileProperties.BasicProperties basicProperties = await file.GetBasicPropertiesAsync();

                if (basicProperties.Size > 0)
                {
                    using (var Stream = await storageFolder.OpenStreamForReadAsync(FileName))
                    {
                        //starts to hang here
                        System.Xml.XmlDictionaryReader XmlReader = System.Xml.XmlDictionaryReader.CreateTextReader(Stream, new System.Xml.XmlDictionaryReaderQuotas());
                        //doesn't get further than here??
                        System.Runtime.Serialization.DataContractSerializer Serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(List <MasterDictionary>));
                        var data = (MasterDictionary)Serializer.ReadObject(XmlReader);
                        DataList.Add(data);
                    }


                    return(DataList);
                }
                else
                {
                    return(new List <MasterDictionary>());
                }

                //return new List<MasterDictionary>();
            }
            else
            {
                //Console.WriteLine("No File Found");
                return(new List <MasterDictionary>());
            }
        }
Esempio n. 13
0
        public async void saveBookmarkCollection()
        {
            setPreviousPosition();

            // Serialize collection
            string json = JsonConvert.SerializeObject(bookmarkStorage);

            // save collection to file
            Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            Windows.Storage.StorageFile   storage       = null;

            if (await storageFolder.TryGetItemAsync(storageFile) == null)
            {
                storage = await storageFolder.CreateFileAsync(storageFile);
            }
            else
            {
                storage = await storageFolder.GetFileAsync(storageFile);
            }

            await Windows.Storage.FileIO.WriteTextAsync(storage, json);
        }
Esempio n. 14
0
        /// <summary>
        /// Tries to get the file or folder with the specified name from the current folder.
        /// Returns null instead of raising a FileNotFoundException if the specified file or folder is not found.
        /// </summary>
        /// <param name="name">The name (or path relative to the current folder) of the file or folder to get.</param>
        /// <returns>When this method completes successfully, it returns an IStorageItem that represents the specified file or folder.
        /// If the specified file or folder is not found, this method returns null instead of raising an exception.</returns>
        public Task <IStorageItem> TryGetItemAsync(string name)
        {
#if WINDOWS_UWP || WINDOWS_APP
            return(Task.Run <IStorageItem>(async() =>
            {
                var i = await _folder.TryGetItemAsync(name);
                if (i == null)
                {
                    return null;
                }
                if (i.IsOfType(Windows.Storage.StorageItemTypes.File))
                {
                    return (StorageFile)((Windows.Storage.StorageFile)i);
                }
                else
                {
                    return (StorageFolder)((Windows.Storage.StorageFolder)i);
                }
            }));
#elif WINDOWS_PHONE_APP
            return(Task.Run <IStorageItem>(async() =>
            {
                if (name.Contains("."))
                {
                    // names containing a . are files so do this faster files-only approach
                    foreach (var file in await _folder.GetFilesAsync())
                    {
                        if (file.Name == name)
                        {
                            return (StorageFile)((Windows.Storage.StorageFile)file);
                        }
                    }
                }
                else
                {
                    // items with no extension could be either file or folder so check all items
                    foreach (Windows.Storage.IStorageItem item in await _folder.GetItemsAsync())
                    {
                        if (item.Name == name)
                        {
                            if (item.IsOfType(Windows.Storage.StorageItemTypes.File))
                            {
                                return (StorageFile)((Windows.Storage.StorageFile)item);
                            }
                            else
                            {
                                return (StorageFolder)((Windows.Storage.StorageFolder)item);
                            }
                        }
                    }
                }

                return null;
            }));
#elif WINDOWS_PHONE
            return(Task.Run <IStorageItem>(async() =>
            {
                string isoPath = _folder.GetIsoStorePath();
                if (isoPath == null)
                {
                    // On 8.1 defer to try/catch if outside of localstate/isostore
                    try
                    {
                        var i = await _folder.GetItemAsync(name);
                        if (i.IsOfType(Windows.Storage.StorageItemTypes.File))
                        {
                            return (StorageFile)((Windows.Storage.StorageFile)i);
                        }
                        else
                        {
                            return (StorageFolder)((Windows.Storage.StorageFolder)i);
                        }
                    }
                    catch
                    {
                        return null;
                    }
                }

                if (name.Contains("."))
                {
                    // file
                    if (IsolatedStorageFile.GetUserStoreForApplication().FileExists(global::System.IO.Path.Combine(isoPath, name)))
                    {
                        return (StorageFile)((Windows.Storage.StorageFile) await _folder.GetFileAsync(name));
                    }
                }
                else
                {
                    // folder
                    if (IsolatedStorageFile.GetUserStoreForApplication().DirectoryExists(global::System.IO.Path.Combine(isoPath, name)))
                    {
                        return (StorageFolder)((Windows.Storage.StorageFolder) await _folder.GetFolderAsync(name));
                    }
                    else if (IsolatedStorageFile.GetUserStoreForApplication().FileExists(global::System.IO.Path.Combine(isoPath, name)))
                    {
                        // file without extension
                        return (StorageFile)((Windows.Storage.StorageFile) await _folder.GetFileAsync(name));
                    }
                }

                return null;
            }));
#elif __ANDROID__ || __UNIFIED__ || WIN32 || TIZEN
            return(Task.Run <IStorageItem>(() =>
            {
                string itempath = global::System.IO.Path.Combine(Path, name);
                if (File.Exists(itempath))
                {
                    return new StorageFile(itempath);
                }
                else if (Directory.Exists(itempath))
                {
                    return new StorageFolder(itempath);
                }

                return null;
            }));
#else
            throw new PlatformNotSupportedException();
#endif
        }
Esempio n. 15
0
        /// <summary>
        /// Opens a saved file (favorites or settings).
        /// </summary>
        /// <param name="fileType"></param>
        /// <param name="filename"></param>
        private async void YtOpenFile(FileType fileType, string filename)
        {
            Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            Windows.Storage.StorageFile   jsonFile      = (Windows.Storage.StorageFile) await storageFolder.TryGetItemAsync(filename);

            if (jsonFile != null)
            {
                string jsonContent = await Windows.Storage.FileIO.ReadTextAsync(jsonFile);

                if (fileType == FileType.FAVORITES && !string.IsNullOrEmpty(jsonContent))
                {
                    YtFavoritesList = JsonConvert.DeserializeObject <ObservableCollection <YtLocalFavChannelList> >(jsonContent);
                }
                else if (fileType == FileType.SETTINGS && !string.IsNullOrEmpty(jsonContent))
                {
                    AppSettings = JsonConvert.DeserializeObject <YtApplicationSettings>(jsonContent);
                }
            }
        }
Esempio n. 16
0
        private async Task <FileAccessPolyFill.StorageFile> TryGetItemAsyncHelper(string name)
        {
            var content = await _folder.TryGetItemAsync(name);

            return(new FileAccessPolyFill.StorageFile((Windows.Storage.StorageFile)content));
        }