public string GetTargetPath(ELImageDeviceElement element) { string strPath = ""; if (string.IsNullOrEmpty(element.TargetPathRoot) || Directory.Exists(element.TargetPathRoot) == false) { return(string.Empty); } string rootPath = element.TargetPathRoot.TrimEnd(Path.DirectorySeparatorChar); string format = element.SourcePathFormat.Trim(Path.DirectorySeparatorChar); DateTime dtFileName = DateTime.Now; //根据测试机早上8点钟才会更换测试数据文件路径的属性,增加下面的判断 if (DateTime.Now >= Convert.ToDateTime(DateTime.Now.ToShortDateString() + " 00:00:00") && DateTime.Now < Convert.ToDateTime(DateTime.Now.ToShortDateString() + " 08:00:00")) { dtFileName = dtFileName.AddDays(-1); } string sourcePath = rootPath + Path.DirectorySeparatorChar + string.Format(format, dtFileName); if (Directory.Exists(sourcePath) == false) { Directory.CreateDirectory(sourcePath); } return(sourcePath); }
public void Execute(ELImageDeviceElement element, String strLot) { strImageFullPath = CommonFun.GetFullPath(element.SourceImagePathRoot, element.SourceImagePathFormat); string strFileName = strImageFullPath + strLot + "." + element.ImageExtensionName; FileInfo fInfo = new FileInfo(strFileName); FtpManager.UploadFile(fInfo, element.FtpTargetFolder, element.FtpServer, element.FtpUser, element.FtpPassword); }
/// <summary> /// 获取数据文件路径。先按照式化字符串中设置的格式寻找,如果没有匹配则返回文件夹中最新的文件夹路径。 /// </summary> /// <param name="element">图片设备配置。</param> /// <returns> /// 图片数据源文件夹路径。 /// </returns> public string GetSourcePath(ELImageDeviceElement element) { if (string.IsNullOrEmpty(element.SourcePathRoot) || Directory.Exists(element.SourcePathRoot) == false) { return(string.Empty); } string rootPath = element.SourcePathRoot.TrimEnd(Path.DirectorySeparatorChar); string format = element.SourcePathFormat.Trim(Path.DirectorySeparatorChar); DateTime dtFileName = DateTime.Now; //根据测试机早上8点钟才会更换测试数据文件路径的属性,增加下面的判断 if (DateTime.Now >= Convert.ToDateTime(DateTime.Now.ToShortDateString() + " 00:00:00") && DateTime.Now < Convert.ToDateTime(DateTime.Now.ToShortDateString() + " 08:00:00")) { dtFileName = dtFileName.AddDays(-1); } string sourcePath = rootPath + Path.DirectorySeparatorChar + string.Format(format, dtFileName); if (Directory.Exists(sourcePath)) { return(sourcePath); } DirectoryInfo TheFolder = new DirectoryInfo(element.SourcePathRoot); DirectoryInfo[] directoryInfos = TheFolder.GetDirectories(); DateTime dtLastWrite = DateTime.MinValue; DirectoryInfo diLastWrite = null; //获取最新的文件夹。 foreach (DirectoryInfo item in directoryInfos) { if (item.LastWriteTime > dtLastWrite) { dtLastWrite = item.LastWriteTime; diLastWrite = item; } } if (diLastWrite != null) { return(diLastWrite.FullName); } else { return(rootPath); } }
private void FrmELImageUpload_Load(object sender, EventArgs e) { label4.Text = username; try { bool blFound = false; string hostInfo = Dns.GetHostName(); System.Net.IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList; //IPAddress ipaddress = addressList[0]; //string ips = ipaddress.ToString(); //MessageBox.Show(ips); //获取配置节信息 this._section = (ELImageConfigurationSection)ConfigurationManager.GetSection("mes.ELImage"); //增加线程个数。 foreach (ELImageDeviceElement element in this._section.Devices) { foreach (IPAddress ipAddress in addressList) { strIP = ipAddress.ToString(); if (strIP == element.Name) { blFound = true; _deviceElement = element; break; } } } if (blFound == false) { MessageBox.Show("此电脑的IP地址没有在配置文件中找到对应的配置,请联系IT"); Application.Exit(); } else { txtSourceImagePath.Text = _deviceElement.SourceImagePathRoot; } } catch (Exception ex) { //throw; } }
public static string UploadFile(string strFileName, ELImageDeviceElement element) { string strMessage = ""; try { if (File.Exists(strFileName)) { FileInfo fInfo = new FileInfo(strFileName); FtpManager.UploadFile(fInfo, element.FtpTargetFolder, element.FtpServer, element.FtpUser, element.FtpPassword); strMessage = string.Format("上传图片({0})成功", strFileName); } else { strMessage = string.Format("图片({0})不存在", strFileName); } } catch (Exception err) { strMessage = err.Message; } return(strMessage); }
/// <summary> /// 将图片数据上传到MES中。 /// </summary> /// <param name="element">图片设备配置。</param> /// <returns>true:转置成功。false:转置失败。</returns> public void Execute(ELImageDeviceElement element) { bool blFindFiles = false; bool blTransferDataResult = true; string strTransferDataMsg = ""; try { //获取源文件夹路径。 string sourcePath = this.GetSourcePath(element); if (string.IsNullOrEmpty(sourcePath)) { blTransferDataResult = false; strTransferDataMsg = string.Format("获取 {0} 源文件夹路径失败。", element.Name); } //获取目标文件夹路径。 string sourceRootPath = element.SourcePathRoot.TrimEnd(Path.DirectorySeparatorChar); string targetRootPath = element.TargetPathRoot.TrimEnd(Path.DirectorySeparatorChar); //string targetPath = sourcePath.Replace(sourceRootPath, targetRootPath); string targetPath = this.GetTargetPath(element); if (Directory.Exists(targetPath) == false) { Directory.CreateDirectory(targetPath); } //获取源文件夹下的未移转图片。 DirectoryInfo diSourcePath = new DirectoryInfo(sourcePath); string searchPattern = string.Format("*.{0}", element.FileExtensionName); FileInfo[] fileInfos = diSourcePath.GetFiles(searchPattern, SearchOption.AllDirectories); if (fileInfos != null && fileInfos.Length > 0) { blFindFiles = true; } DateTime dtMaxTime = DateTime.MinValue; //遍历文件夹文件。 int maxNumberForLoop = 10; int nIndexOfFile = 0; foreach (FileInfo fiItem in fileInfos) { if (nIndexOfFile > maxNumberForLoop) { break; } nIndexOfFile = nIndexOfFile + 1; string targetFileName = fiItem.FullName.Replace(sourcePath, targetPath); Directory.CreateDirectory(Path.GetDirectoryName(targetFileName)); //复制源文件到目标文件夹下。 File.Copy(fiItem.FullName, targetFileName, true); dtMaxTime = fiItem.LastWriteTime.AddMilliseconds(1); #region //进行原文件的后续处理。 int count = 0; while (count < 5) { count++; try { //如设置删除源文件,则删除。 if (element.IsDeleteSourceFile) { File.Delete(fiItem.FullName); } else { //否则,移动到LocalFiles下。 //string copyFilePath = sourcePath.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar + "LocalFiles"; //if (Directory.Exists(copyFilePath) == false) //{ // Directory.CreateDirectory(copyFilePath); //} //string copyFileName = fiItem.FullName.Replace(sourcePath, copyFilePath); //File.Copy(fiItem.FullName, copyFileName,true); //File.Delete(fiItem.FullName); } break; } catch (Exception ex) { LogMessage(false, ex.Message); System.Threading.Thread.Sleep(500); continue; } } #endregion } } catch (Exception ex) { blTransferDataResult = false; LogMessage(false, ex.Message); } if (blFindFiles == false) { strTransferDataMsg = ""; LogMessage(true, strTransferDataMsg); } }