啊鑫
2024-10-25 cefcc903f51610846fa313a3a35bca34e129c1fe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System.Windows.Forms;
 
namespace Gs.DevApp.DevFrm.Sys
{
    public enum FileType
    {
        Txt,
        Xml,
        Pdf,
        Bin,
        Zip,
        All,
        Img,
        Excel
    }
 
    public class FileSelector
    {
        public static string Title = "Please choose the file:";
 
        public static string Filter =
            "Text files (*.txt)|*.txt|All files (*.*)|*.*";
 
        /// <summary>
        ///     Get or set the initial directory displayed by the file dialog box.
        /// </summary>
        public static OpenFileDialog OFD = new OpenFileDialog();
 
        public static SaveFileDialog SFD = new SaveFileDialog();
 
        public static FileType FileExtension
        {
            set
            {
                switch (value)
                {
                    case FileType.Txt:
                        Filter = "Text files(*.txt)|*.txt|All files(*.*)|*.*";
                        break;
                    case FileType.Excel:
                        Filter =
                            "Text files(*.xls;*.xlsx)|*.xls;*.xlsx|All files(*.*)|*.*";
                        break;
                    case FileType.Xml:
                        Filter =
                            "XML files(*.xml)|*.xml|Config files(*.config)|*.config|All files(*.*)|*.*";
                        break;
                    case FileType.Pdf:
                        Filter =
                            "Pdf files(*.Pdf)|*.Pdf|Pdf form files(*.fdf)|*.fdf|All files(*.*)|*.*";
                        break;
                    case FileType.Bin:
                        Filter =
                            "Binary files(*.bin)|*.bin|Application files(*.exe;*.lib;*.dll)|*.exe;*.lib;*.dll|All files(*.*)|*.*";
                        break;
                    case FileType.Zip:
                        Filter =
                            "Zip files(*.zip;*.rar)|*.zip;*.rar|All files(*.*)|*.*";
                        break;
                    case FileType.Img:
                        Filter =
                            "GIF(*.gif)|*.gif|Jpeg(*.jpg)|*.jpg|Bmp(*.bmp)|*.bmp|Emf(*.emf)|*.emf|Png(*.png)|*.png";
                        break;
                    case FileType.All:
                        Filter = "All files(*.*)|*.*";
                        break;
                }
            }
        }
 
        public static string InitialPath
        {
            set
            {
                OFD.InitialDirectory = value;
                SFD.InitialDirectory = value;
            }
        }
 
        public static string BrowseFile()
        {
            OFD.Title = Title;
            OFD.Filter = Filter;
            if (OFD.ShowDialog() == DialogResult.OK)
                return OFD.FileName;
            return null;
        }
 
        public static string BrowseFileForSave()
        {
            SFD.Title = Title;
            SFD.Filter = Filter;
            if (SFD.ShowDialog() == DialogResult.OK)
                return SFD.FileName;
            return null;
        }
 
        public static string BrowseFile(FileType type)
        {
            FileExtension = type;
            return BrowseFile();
        }
 
        public static string BrowseFile(string filter)
        {
            Filter = filter;
            return BrowseFile();
        }
 
        public static string BrowseFileForSave(FileType type)
        {
            FileExtension = type;
            return BrowseFileForSave();
        }
 
        public static string BrowseFileForSave(string filter)
        {
            Filter = filter;
            return BrowseFileForSave();
        }
 
        public static string BrowseFilePathForSave()
        {
            var fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK)
                return fbd.SelectedPath; //获得选择的文件夹路径
            return "";
        }
    }
}