Ejemplo n.º 1
0
        public static void PreviewShow(ResourceManagerTreeNode fileInfo)
        {
            if (FilePreviewWindow.showWindows.ContainsKey(fileInfo.Path.FullName))
            {
                FilePreviewWindow.showWindows[fileInfo.Path.FullName].Activate();
                return;
            }
            Application.Current.MainWindow.Cursor = Cursors.Wait;
            if (!(fileInfo.Path.Extension.Equals(".txt", StringComparison.OrdinalIgnoreCase)
                  ||  fileInfo.Path.Extension.Equals(".dat", StringComparison.OrdinalIgnoreCase)
                  || fileInfo.Path.Extension.Equals(".grd", StringComparison.OrdinalIgnoreCase)))
            {
                MessageWindow.Show(Application.Current.MainWindow,"只能预览txt/dat/grd文件!");
                Application.Current.MainWindow.Cursor = Cursors.Arrow;
                return;
            }
            FilePreviewWindow fpw = new FilePreviewWindow(fileInfo.Path.FullName);
            fpw.Title = fileInfo.Path.Name;
            fpw.fileName.Text = fileInfo.Path.Name;
            fpw.fileName.ToolTip = fileInfo.Path.FullName;

                try
                {
                    using (var stream = new StreamReader(fileInfo.Path.FullName, Encoding.Default))
                    {
                        StringBuilder result = new StringBuilder(stream.ReadToEnd());
                        stream.Close();
                        fpw.Dispatcher.Invoke(
                            new Action(() =>
                            {
                                fpw.fileContent.AppendText(result.ToString());
                            }));
                    }
                }
                catch (Exception)
                {
                    fpw.Dispatcher.Invoke(
                            new Action(() =>
                            {
                                MessageWindow.Show(fpw, "读取文件失败!");
                                fpw.Close();
                            }));
                }
                fpw.Show();
                showWindows.Add(fileInfo.Path.FullName, fpw);
                Application.Current.MainWindow.Cursor = Cursors.Arrow;
        }
Ejemplo n.º 2
0
        public static void PreviewShow(Window owner, FileSystemInfo fileInfo)
        {
            Application.Current.MainWindow.Cursor = Cursors.Wait;
            if (!(fileInfo.Extension.Equals(".txt", StringComparison.OrdinalIgnoreCase)
                  || fileInfo.Extension.Equals(".dat", StringComparison.OrdinalIgnoreCase)
                  || fileInfo.Extension.Equals(".grd", StringComparison.OrdinalIgnoreCase)))
            {
                MessageWindow.Show(Application.Current.MainWindow, "只能预览txt/dat/grd文件!");
                Application.Current.MainWindow.Cursor = Cursors.Arrow;
                return;
            }
            FilePreviewWindow fpw = new FilePreviewWindow(fileInfo.FullName);
            fpw.Owner = owner;
            fpw.Title = "文本预览";
            fpw.fileName.Text = "";
            fpw.minBtn.Visibility = Visibility.Hidden;
            fpw.fileName.ToolTip = fileInfo.FullName;

            try
            {
                using (var stream = new StreamReader(fileInfo.FullName, Encoding.Default))
                {
                    StringBuilder result = new StringBuilder(stream.ReadToEnd());
                    stream.Close();
                    fpw.Dispatcher.Invoke(
                        new Action(() =>
                        {
                            fpw.fileContent.AppendText(result.ToString());
                        }));
                }
            }
            catch (Exception)
            {
                fpw.Dispatcher.Invoke(
                        new Action(() =>
                        {
                            MessageWindow.Show(fpw, "读取文件失败!");
                            fpw.Close();
                        }));
            }
            fpw.ShowInTaskbar = false;
            fpw.ShowDialog();
            Application.Current.MainWindow.Cursor = Cursors.Arrow;
        }