Преглед на файлове

其他-补充工具方法:文件路径判重,追加尾号;文件夹路径判重,追加尾号;调用系统文件系统

ZhouJieSheng преди 2 години
родител
ревизия
71a60e29f2
променени са 3 файла, в които са добавени 150 реда и са изтрити 0 реда
  1. 1 0
      PDF Office/CustomControl/AlertsMessage.xaml
  2. 66 0
      PDF Office/CustomControl/DialogContent.cs
  3. 83 0
      PDF Office/Helper/CommonHelper.cs

+ 1 - 0
PDF Office/CustomControl/AlertsMessage.xaml

@@ -20,6 +20,7 @@
         Background="White"
         BorderThickness="1"
         CornerRadius="6"
+        BorderBrush="Gray"
         MouseLeftButtonDown="Border_PreviewMouseLeftButtonDown">
         <Grid Margin="20,24,20,0">
             <Grid.RowDefinitions>

+ 66 - 0
PDF Office/CustomControl/DialogContent.cs

@@ -0,0 +1,66 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Navigation;
+using System.Windows.Shapes;
+
+namespace PDF_Office.CustomControl
+{
+    /// <summary>
+    /// 不能使用usercontrol来实现该模块 会导致content里的控件无法命名
+    /// 需要创建自定义控件 customcontrol类型
+    /// </summary>
+    public class DialogContent : Control
+    {
+        static DialogContent()
+        {
+            DefaultStyleKeyProperty.OverrideMetadata(typeof(DialogContent), new FrameworkPropertyMetadata(typeof(DialogContent)));
+        }
+
+        /// <summary>
+        /// 弹窗标题
+        /// </summary>
+        public string Header
+        {
+            get { return (string)GetValue(HeaderProperty); }
+            set { SetValue(HeaderProperty, value); }
+        }
+
+        // Using a DependencyProperty as the backing store for Header.  This enables animation, styling, binding, etc...
+        public static readonly DependencyProperty HeaderProperty =
+            DependencyProperty.Register("Header", typeof(string), typeof(DialogContent), new PropertyMetadata(""));
+
+
+
+        public object BottmBar
+        {
+            get { return (object)GetValue(BottmBarProperty); }
+            set { SetValue(BottmBarProperty, value); }
+        }
+
+        // Using a DependencyProperty as the backing store for BottmBar.  This enables animation, styling, binding, etc...
+        public static readonly DependencyProperty BottmBarProperty =
+            DependencyProperty.Register("BottmBar", typeof(object), typeof(DialogContent), new PropertyMetadata(null));
+
+
+
+        public object Content
+        {
+            get { return (object)GetValue(ContentProperty); }
+            set { SetValue(ContentProperty, value); }
+        }
+
+        // Using a DependencyProperty as the backing store for Content.  This enables animation, styling, binding, etc...
+        public static readonly DependencyProperty ContentProperty =
+            DependencyProperty.Register("Content", typeof(object), typeof(DialogContent), new PropertyMetadata(null));
+    }
+}

+ 83 - 0
PDF Office/Helper/CommonHelper.cs

@@ -1,5 +1,7 @@
 using System;
 using System.Collections.Generic;
+using System.Diagnostics;
+using System.IO;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
@@ -13,6 +15,9 @@ namespace PDF_Office.Helper
     /// 2 FindVisualChild  查找目标类型的子类控件
     /// 3 GetPageParmFromList  从页码集合获取页码字符串,如1,2,3 转换成1-3
     /// 4 GetPagesInRange 校验PageRange 输入是否合法,且可返回List<int> Pages  存放的索引值
+    /// 5.ShowFileBrowser 显示系统文件浏览器,可以根据传入的路径参数,自动选中对应的文件
+    /// 6.CreateFilePath 检查对应路径是否有重名,有重名的情况追加尾号
+    /// 7.CreateFolder 检查对应文件夹是否有重名,有重名的情况追加尾号
     /// </summary>
     public static class CommonHelper
     {
@@ -194,5 +199,83 @@ namespace PDF_Office.Helper
             return true;
         }
 
+        /// <summary>
+        /// 显示系统文件浏览器
+        /// </summary>
+        /// <param name="selectedFile">要选中的文件路径</param>
+        public static void ShowFileBrowser(string selectedFile=null)
+        {
+            if(string.IsNullOrEmpty(selectedFile))
+            {
+                Process.Start(@"explorer.exe");
+            }
+            else
+            {
+                Process.Start(@"explorer.exe", "/select,\"" + selectedFile + "\"");
+            }
+    
+        }
+
+
+        /// <summary>
+        /// 检测文件是否重复  追加尾号
+        /// </summary>
+        /// <param name="path"></param>
+        /// <returns></returns>
+        public static string CreateFilePath(string path)
+        {
+            int i = 1;
+            string oldDestName = path;
+            do
+            {
+                if (File.Exists(path))
+                {
+                    int lastDot = oldDestName.LastIndexOf('.');
+
+                    string fileExtension = string.Empty;
+
+                    string fileName = oldDestName;
+
+                    if (lastDot > 0)
+                    {
+                        fileExtension = fileName.Substring(lastDot);
+
+                        fileName = fileName.Substring(0, lastDot);
+                    }
+
+                    path = fileName + string.Format(@"({0})", i) + fileExtension;
+                }
+                ++i;
+
+            } while (File.Exists(path));
+            return path;
+        }
+
+        /// <summary>
+        /// 检查文件夹是否重复  追加尾号
+        /// </summary>
+        /// <param name="path"></param>
+        /// <returns></returns>
+        public static string CreateFolder(string folder)
+        {
+            int i = 1;
+            string oldDestName = folder;
+            DirectoryInfo info = new DirectoryInfo(folder);
+            do
+            {
+                info = new DirectoryInfo(folder);
+                if (info.Exists)
+                {
+
+                    string fileName = oldDestName;
+
+                    folder = fileName + string.Format(@"({0})", i);
+                }
+                ++i;
+
+            } while (info.Exists);
+            info.Create();
+            return folder;
+        }
     }
 }