// Helper method: get parent directory IsoDirectory GetParentDir(IsoDirectory root, IsoFile child) { if (string.IsNullOrEmpty(child.FilePath)) { return(null); } else { string[] dirs = child.FilePath.Split('/'); IsoDirectory cur = root; for (int i = 1; i < dirs.Length - 1; i++) { IsoDirectory next = cur.Children.FirstOrDefault(dir => dir.FileName == dirs[i]) as IsoDirectory; if (next != null) { cur = next; } else { return(null); } } return(cur); } }
// Add File private void bnAddFile_Click(object sender, RoutedEventArgs e) { bool overrideflag = false; var selecteddir = tvIsolatedStorage.SelectedItem as IsoDirectory; if (selecteddir == null) { return; } OpenFileDialog ofd = new OpenFileDialog(); var result = ofd.ShowDialog(); if (result.HasValue && result.Value) { string filename = ofd.File.Name; string filepath = selecteddir.FilePath + "/" + filename; IsoFile file = new IsoFile(filename, filepath); try { // Check if file name is same with directory name if (_isofile.GetDirectoryNames(filepath).Length > 0) { MessageBox.Show(string.Format("File name {0} not allowed", filename)); return; } // Check if file name already exist else if (_isofile.GetFileNames(filepath).Length > 0) { // Show message box, ask user if override file var mbresult = MessageBox.Show(string.Format("Override the current file: {0} ?", filename), "override warning", MessageBoxButton.OKCancel); if (mbresult != MessageBoxResult.OK) { return; } else { overrideflag = true; } } } catch (PathTooLongException) { MessageBox.Show("Add file failed.\nThe file path is too long."); return; } catch (Exception ex) { MessageBox.Show(ex.Message); return; } // Check if has enough space if (_isofile.AvailableFreeSpace < ofd.File.Length) { MessageBox.Show("Not enough isolated storage space."); return; } Stream isostream = null; Stream filestream = null; try { // Create isolatedstorage stream isostream = _isofile.CreateFile(filepath); // Open file stream filestream = ofd.File.OpenRead(); // Copy // Note: exception in copy progress won't be catched here. CopyStream(filestream, isostream); // Check override if (!overrideflag) { selecteddir.Children.Add(file); } } catch (Exception ex) { if (isostream != null) { isostream.Close(); } if (filestream != null) { filestream.Close(); } MessageBox.Show(ex.Message); } } }
// 增加文件 private void bnAddFile_Click(object sender, RoutedEventArgs e) { bool overrideflag = false; var selecteddir = tvIsolatedStorage.SelectedItem as IsoDirectory; if(selecteddir==null) return; OpenFileDialog ofd = new OpenFileDialog(); var result = ofd.ShowDialog(); if (result.HasValue && result.Value) { string filename = ofd.File.Name; string filepath = selecteddir.FilePath + "/" + filename; IsoFile file = new IsoFile(filename, filepath); try { // 检查文件名是否和目录名一样 if (_isofile.GetDirectoryNames(filepath).Length > 0) { MessageBox.Show(string.Format("文件名“{0}”不允许。", filename)); return; } // 检查文件是否已经存在 else if (_isofile.GetFileNames(filepath).Length > 0) { // 显示提示框,问是否覆盖文件 var mbresult = MessageBox.Show(string.Format("覆盖当前文件: {0} ?", filename), "覆盖警告", MessageBoxButton.OKCancel); if (mbresult != MessageBoxResult.OK) return; else overrideflag = true; } } catch (PathTooLongException) { MessageBox.Show("增加文件失败。\n文件路径太长。"); return; } catch (Exception ex) { MessageBox.Show(ex.Message); return; } // 检查是否有足够空间 if (_isofile.AvailableFreeSpace < ofd.File.Length) { MessageBox.Show("独立存储器空间不够。"); return; } Stream isostream = null; Stream filestream = null; try { // 创建isolatedstorage流 isostream = _isofile.CreateFile(filepath); // 打开文件流 filestream = ofd.File.OpenRead(); // 复制 // 注意:这里不会捕捉复制过程中的异常。 CopyStream(filestream, isostream); // 检查覆盖 if (!overrideflag) selecteddir.Children.Add(file); } catch(Exception ex) { if (isostream != null) isostream.Close(); if (filestream != null) filestream.Close(); MessageBox.Show(ex.Message); } } }
// Helper 方法: 取得父目录 IsoDirectory GetParentDir(IsoDirectory root, IsoFile child) { if (string.IsNullOrEmpty(child.FilePath)) return null; else { string[] dirs = child.FilePath.Split('/'); IsoDirectory cur = root; for (int i = 1; i < dirs.Length - 1; i++) { IsoDirectory next = cur.Children.FirstOrDefault(dir => dir.FileName == dirs[i]) as IsoDirectory; if (next != null) cur = next; else return null; } return cur; } }
// 增加文件 private void bnAddFile_Click(object sender, RoutedEventArgs e) { bool overrideflag = false; var selecteddir = tvIsolatedStorage.SelectedItem as IsoDirectory; if (selecteddir == null) { return; } OpenFileDialog ofd = new OpenFileDialog(); var result = ofd.ShowDialog(); if (result.HasValue && result.Value) { string filename = ofd.File.Name; string filepath = selecteddir.FilePath + "/" + filename; IsoFile file = new IsoFile(filename, filepath); try { // 检查文件名是否和目录名一样 if (_isofile.GetDirectoryNames(filepath).Length > 0) { MessageBox.Show(string.Format("文件名“{0}”不允许。", filename)); return; } // 检查文件是否已经存在 else if (_isofile.GetFileNames(filepath).Length > 0) { // 显示提示框,问是否覆盖文件 var mbresult = MessageBox.Show(string.Format("覆盖当前文件: {0} ?", filename), "覆盖警告", MessageBoxButton.OKCancel); if (mbresult != MessageBoxResult.OK) { return; } else { overrideflag = true; } } } catch (PathTooLongException) { MessageBox.Show("增加文件失败。\n文件路径太长。"); return; } catch (Exception ex) { MessageBox.Show(ex.Message); return; } // 检查是否有足够空间 if (_isofile.AvailableFreeSpace < ofd.File.Length) { MessageBox.Show("独立存储器空间不够。"); return; } Stream isostream = null; Stream filestream = null; try { // 创建isolatedstorage流 isostream = _isofile.CreateFile(filepath); // 打开文件流 filestream = ofd.File.OpenRead(); // 复制 // 注意:这里不会捕捉复制过程中的异常。 CopyStream(filestream, isostream); // 检查覆盖 if (!overrideflag) { selecteddir.Children.Add(file); } } catch (Exception ex) { if (isostream != null) { isostream.Close(); } if (filestream != null) { filestream.Close(); } MessageBox.Show(ex.Message); } } }