//将Word文档转为HTML网页形式 public static void ConvertDocToHtml(string SourcePath, string TargetPath) { try { Aspose.Words.Document d = new Aspose.Words.Document(SourcePath); d.Save(TargetPath, SaveFormat.Html); } catch (Exception ex) { Console.WriteLine(ex.Message); log.Info(AppUtil.getExceptionInfo(ex)); } }
/// <summary> /// 复制文件夹 /// </summary> /// <param name="sourceFolderName">源文件夹目录</param> /// <param name="destFolderName">目标文件夹目录</param> /// <param name="overwrite">允许覆盖文件</param> public static bool Copy(string sourceFolderName, string destFolderName, bool overwrite) { if (Directory.Exists(sourceFolderName)) { try { var sourceFilesPath = Directory.GetFileSystemEntries(sourceFolderName); for (int i = 0; i < sourceFilesPath.Length; i++) { var sourceFilePath = sourceFilesPath[i]; var directoryName = Path.GetDirectoryName(sourceFilePath); var forlders = directoryName.Split('\\'); var lastDirectory = forlders[forlders.Length - 1]; var dest = Path.Combine(destFolderName, lastDirectory); if (File.Exists(sourceFilePath)) { var sourceFileName = Path.GetFileName(sourceFilePath); if (!Directory.Exists(dest)) { Directory.CreateDirectory(dest); } File.Copy(sourceFilePath, Path.Combine(dest, sourceFileName), overwrite); } else { if (!Copy(sourceFilePath, dest, overwrite)) { return(false); } } } } catch (Exception ex) { Console.WriteLine(ex.Message); log.Info(AppUtil.getExceptionInfo(ex)); return(false); } } else { return(false); } return(true); }
//拷贝文件到特定的目录下, SourceFilePath-源文件的全路径名, TargetDirPath-目标目录路径 public static bool CopyFileTo(string SourceFilePath, string TargetDirPath) { if (File.Exists(SourceFilePath) && Directory.Exists(TargetDirPath)) //若源文件 和 目标路径都存在 { string TargetFilePath = TargetDirPath + "\\" + Path.GetFileName(SourceFilePath); //目标文件的全路径名 if (!File.Exists(TargetFilePath)) //若目标目录下没有同名的文件 { FileInfo file = new FileInfo(SourceFilePath); try { file.CopyTo(TargetFilePath, true); // true is overwrite return(true); } catch (Exception ex) { Console.WriteLine(ex.Message); log.Info(AppUtil.getExceptionInfo(ex)); } } else { MessageBox.Show("已存在一个名为 " + Path.GetFileName(SourceFilePath) + " 的文件"); } } return(false); }
//Http下载文件, retryNum为重试次数 public static bool HttpDownloadFile(string URLAddress, string filePath, int retryNum) { bool res = false; if (URLAddress != null && filePath != null) { for (int i = 0; i < retryNum; i++) { try { WebClient wc = new WebClient(); //若文件存在则删除 if (File.Exists(filePath)) { File.Delete(filePath); } //若目录不存在则创建目录 if (!Directory.Exists(Path.GetDirectoryName(filePath))) { Directory.CreateDirectory(Path.GetDirectoryName(filePath)); } wc.DownloadFile(URLAddress, filePath); res = true; break; } catch (Exception ex) { Console.WriteLine(ex.Message); log.Info(AppUtil.getExceptionInfo(ex)); res = false; } } } return(res); }
/* * 查找包含多个关键字符串的第一个串口 * 例如要找出CH340串口号是多少: 其串口名称为"USB-SERIAL CH340 (COM8)", 关键字符串为"SERIAL"和"CH340", 则函数返回"COM8" */ public static string FindComByKeyStr(string[] keyStr) { string comName = null; //串口枚举, 获取每个串口名称的字符串数组 if (keyStr != null) { string[] CurSerialPort = SerialEnumPort.enumSerialPortGetName(); if (CurSerialPort != null) { foreach (string str in CurSerialPort) { bool isContanKey = true; foreach (string ketstr in keyStr) { if (!str.Contains(ketstr)) { isContanKey = false; break; } } if (isContanKey) //若名称包含关键字符串 { int leftBracketIndex = 0; try { leftBracketIndex = str.LastIndexOf("("); } catch (Exception ex) { Console.WriteLine(ex.Message); log.Info(AppUtil.getExceptionInfo(ex)); } if (leftBracketIndex != 0) { int tmp = 0; if (int.TryParse(str.Substring(leftBracketIndex + 5, 1), out tmp)) { comName = str.Substring(leftBracketIndex + 1, 5); } else { comName = str.Substring(leftBracketIndex + 1, 4); } break; } } } } } return(comName); }