Beispiel #1
0
        public async Task CalculateSize()
        {
            try
            {
                if (SizeIsCalculated)
                {
                    return;
                }

                string        path   = TheApp.PackageDataFolder;
                StorageFolder folder = await StorageFolder.GetFolderFromPathAsync(path);//PackageRootFolder.ToLower().Replace("c:\\data\\","u:\\"));

                List <IStorageItem> contents = await FileOperations.GetContents(folder);

                List <StorageFile> files = (from IStorageItem s in contents
                                            where s is StorageFile
                                            select(StorageFile) s).ToList();

                double sizeBytes = await FileOperations.GetSizeOfFiles(files);

                AppDataSize      = FileOperations.GetFileSizeString(sizeBytes);
                SizeIsCalculated = true;
                NotifyChange();
            }
            catch
            {
                AppDataSize = "Unknown";
            }
        }
        public async Task ResetAppData(AppData app)
        {
            StorageFolder dataFolder = await StorageFolder.GetFolderFromPathAsync(app.PackageDataFolder);

            List <StorageFile> files = (from IStorageItem s in (await FileOperations.GetContents(dataFolder))
                                        where s is StorageFile
                                        select(StorageFile) s).ToList();

            int count   = files.Count;
            int current = 0;

            foreach (var item in files)
            {
                try
                {
                    string relativePath = item.Path.Substring(app.PackageDataFolder.Length + 1).Replace('/', '\\');
                    if (!relativePath.Contains("\\"))
                    {
                        continue;
                    }

                    relativePath = relativePath.Substring(0, relativePath.IndexOf("\\"));

                    if (deletableFolders.Contains(relativePath))
                    {
                        await item.DeleteAsync();
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message + " :: " + item.Path);
                }
                current++;

                OnBackupProgress(new BackupEventArgs(100.0 * ((double)current) / count, BackupState.ResettingAppData2, current.ToString() + " / " + count.ToString(), "", null));
            }

            OnBackupProgress(new BackupEventArgs(100.0, BackupState.Finalizing2, current.ToString() + " / " + count.ToString(), "", null));
            App.GetAppDataEx(app).ResetSizeData();
        }
            public async Task <List <string> > Copy()
            {
                List <string> log = new List <string>();

                int srcAddressBeginIndex = _source.Path.LastIndexOf('\\') + 1;

                if (_items == null) //Needs loading
                {
                    OnCopying(new CopyingEventArgs(0, 0, 0, 0, log));
                    _items = await FileOperations.GetContents(_source);
                }

                try
                {
                    await _dest.CreateFolderAsync(_source.Path.Substring(srcAddressBeginIndex));
                }
                catch (Exception ex)
                {
                    throw new Exception("Cannot access disk, please change the backup location from settings and try again. (" + ex.Message + ")");
                }

                List <StorageFolder> folders = (from IStorageItem s in _items
                                                where s is StorageFolder
                                                select(StorageFolder) s).OrderBy(x => x.Path.Count(y => y == '\\')).ToList();
                int foldersCount = folders.Count;


                List <StorageFile> files = (from IStorageItem s in _items
                                            where s is StorageFile
                                            select(StorageFile) s).ToList();
                int filesCount = files.Count;

                int progress = 0;

                foreach (var item in folders)
                {
                    string        destFolder = System.IO.Path.Combine(_dest.Path, item.Path.Substring(srcAddressBeginIndex));
                    string        parentPath = System.IO.Path.GetDirectoryName(destFolder);
                    StorageFolder parent     = await StorageFolder.GetFolderFromPathAsync(parentPath);

                    await parent.CreateFolderAsync(System.IO.Path.GetFileName(destFolder));

                    progress++;
                    OnCopying(new CopyingEventArgs(0, filesCount, progress, foldersCount, log));
                }

                progress = 0;
                foreach (var item in files)
                {
                    try
                    {
                        string        destFile   = System.IO.Path.Combine(_dest.Path, item.Path.Substring(srcAddressBeginIndex));
                        string        parentPath = System.IO.Path.GetDirectoryName(destFile);
                        StorageFolder destFolder = await StorageFolder.GetFolderFromPathAsync(parentPath);

                        await item.CopyAsync(destFolder);

                        progress++;
                        OnCopying(new CopyingEventArgs(progress, filesCount, foldersCount, foldersCount, log));
                    }
                    catch (Exception ex)
                    {
                        log.Add("Can't copy " + item.Path.Substring(srcAddressBeginIndex) + ": " + ex.Message);
                    }
                }

                return(log);
            }