1
// ------------------------------------------------------------------
2 // Copyright (C) 2011-2015 Maruko Toolbox Project
4 // Authors: LYF <lyfjxymf@sina.com>
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
15 // express or implied.
16 // See the License for the specific language governing permissions
17 // and limitations under the License.
18 // -------------------------------------------------------------------
22 using System
.Collections
.Generic
;
25 using System
.Diagnostics
;
26 using System
.Windows
.Forms
;
28 using System
.Threading
;
29 using System
.Net
.NetworkInformation
;
30 using System
.Runtime
.InteropServices
;
39 /// <param name="path"></param>
40 /// <returns></returns>
41 public static string FormatPath(string path
)
43 if (string.IsNullOrEmpty(path
)) { return null; }
45 ret
= path
.Replace("\"", "");
46 if (ret
.Contains(" ")) { ret = "\"" + ret + "\""; }
51 /// concat:D:\一二三123.png 可以防止FFMpeg不认中文名输入文件的Bug
53 /// <param name="path"></param>
54 /// <returns></returns>
55 public static string ConcatProtocol(string path
)
57 return FormatPath("concat:" + path
);
63 /// <param name="oriPath_">原路径</param>
64 /// <returns>不会重名的路径</returns>
65 public static string GetSuitablePath(string oriPath_
)
67 if (string.IsNullOrEmpty(oriPath_
)) { return null; }
68 string oriPath
= oriPath_
.Replace("\"", string.Empty
);//去掉引号
70 if ((!File
.Exists(oriPath
)) && (!Directory
.Exists(oriPath
)))
75 string ext
= Path
.GetExtension(oriPath
);
76 string str
= oriPath
.Remove(oriPath
.Length
- ext
.Length
) + "_" + i
;
77 while (File
.Exists(str
+ ext
) || Directory
.Exists(str
+ ext
))
80 str
= oriPath
+ "_" + i
;
87 /// <para></para>输入:目标文件的 路径或所在目录;
88 /// <para></para>输入:原始文件的路径;
89 /// <para></para>输出:输出文件的路径。
90 /// <para></para>用目标目录或文件路径(取自tbOutput),和输入的文件,返回一个供输出的文件路径。
92 /// <param name="DestDirOrFile_">目标目录或文件路径</param>
93 /// <param name="SrcFile_">输入的文件(若DestDirOrFile是文件,则忽略此项)</param>
94 /// <param name="dotExtension">换扩展名</param>
95 /// <returns></returns>
96 public static string GetSimilarFilePath(string DestDirOrFile_
, string SrcFile_
, string dotExtension
)
98 if (string.IsNullOrEmpty(DestDirOrFile_
)) { return null; }
99 if (string.IsNullOrEmpty(SrcFile_
)) { return null; }
100 string DestDirOrFile
= DestDirOrFile_
.Replace("\"", string.Empty
);
101 string SrcFile
= SrcFile_
.Replace("\"", string.Empty
);//去掉引号
103 if (DestDirOrFile
.EndsWith("\\"))//目录
105 if (string.IsNullOrEmpty(dotExtension
))//没有指定扩展名
107 return DestDirOrFile
+ Path
.GetFileName(SrcFile
);
111 return DestDirOrFile
+ Path
.GetFileNameWithoutExtension(SrcFile
) + dotExtension
;
119 if (string.IsNullOrEmpty(dotExtension
))//没有指定扩展名
121 return DestDirOrFile
;
125 return ChangeExt(DestDirOrFile
, dotExtension
);//换扩展名
131 /// 用目标目录或文件路径(取自tbOutput),和输入的文件,返回一个供输出的文件路径。
133 /// <param name="DestDirOrFile">目标目录或文件路径</param>
134 /// <param name="SrcFile">输入的文件</param>
135 /// <returns></returns>
136 public static string GetSimilarFilePath(string DestDirOrFile
, string SrcFile
)
138 return GetSimilarFilePath(DestDirOrFile
, SrcFile
, null);
144 /// <param name="srcFile"></param>
145 /// <param name="ext"></param>
146 /// <returns></returns>
147 public static string ChangeExt(string srcFile
, string ext
)
149 return GetDir(srcFile
) + Path
.GetFileNameWithoutExtension(srcFile
) + ext
;
155 /// <param name="path">文件路径</param>
156 /// <returns></returns>
157 public static string GetDir(string path
)
159 string fileDir
= Path
.GetDirectoryName(path
);
160 if (fileDir
.EndsWith("\\"))
166 return fileDir
+ "\\";
170 public static void DeleteDirectoryIfExists(string path
, bool recursive
)
172 if (Directory
.Exists(path
))
173 Directory
.Delete(path
, recursive
);
176 public static DirectoryInfo
ensureDirectoryExists(string path
)
178 if (Directory
.Exists(path
))
179 return new DirectoryInfo(path
);
180 if (string.IsNullOrEmpty(path
))
181 throw new IOException("无法创建目录");
182 ensureDirectoryExists(GetDirectoryName(path
));
183 System
.Threading
.Thread
.Sleep(100);
184 return Directory
.CreateDirectory(path
);
187 public static string GetDirectoryName(string file
)
189 string path
= string.Empty
;
192 path
= Path
.GetDirectoryName(file
);
199 /// Gets the file version/date
201 /// <param name="fileName">the file to check</param>
202 /// <param name="fileVersion">the file version</param>
203 /// <param name="fileDate">the file date</param>
204 /// <param name="fileProductName">the file product name</param>
205 /// <returns>true if file can be found, false if file cannot be found</returns>
206 public static bool GetFileInformation(string fileName
, out string fileVersion
, out string fileDate
, out string fileProductName
)
208 fileVersion
= fileDate
= fileProductName
= string.Empty
;
209 if (!File
.Exists(fileName
))
212 FileVersionInfo FileProperties
= FileVersionInfo
.GetVersionInfo(fileName
);
213 fileVersion
= FileProperties
.FileVersion
;
214 if (!String
.IsNullOrEmpty(fileVersion
))
215 fileVersion
= fileVersion
.Replace(", ", ".");
216 fileDate
= File
.GetLastWriteTimeUtc(fileName
).ToString("dd-MM-yyyy");
217 fileProductName
= FileProperties
.ProductName
;
222 [DllImport("wininet.dll")]
223 private extern static bool InternetGetConnectedState(int Description
, int ReservedValue
);
225 /// 用于检查网络是否可以连接互联网,true表示连接成功,false表示连接失败
227 /// <returns></returns>
228 public static bool IsConnectInternet()
231 return InternetGetConnectedState(Description
, 0);
235 /// ffmpeg output wrapper
237 /// <param name="workPath">work path which contains ffmpeg</param>
238 /// <param name="filename">target media file</param>
239 /// <returns>ffmpeg output info</returns>
240 public static string GetFFmpegOutput(string workPath
, string filename
)
242 var processInfo
= new System
.Diagnostics
.ProcessStartInfo(
243 System
.IO
.Path
.Combine(workPath
, "ffmpeg.exe"), "-i " + FormatPath(filename
));
244 processInfo
.WorkingDirectory
= System
.IO
.Directory
.GetCurrentDirectory();
245 processInfo
.CreateNoWindow
= true;
246 processInfo
.UseShellExecute
= false;
247 processInfo
.RedirectStandardError
= true;
248 var proc
= System
.Diagnostics
.Process
.Start(processInfo
);
249 string output
= proc
.StandardError
.ReadToEnd();