private bool FilesExist(string userName, string password, string path, List <string> files) { bool exists = true; string connectResult = String.Empty; //connect to share if username and password is required if (!String.IsNullOrEmpty(userName) && !String.IsNullOrEmpty(password)) { connectResult = PInvokeWindowsNetworking.ConnectToRemote(path, userName, password); if (String.IsNullOrEmpty(connectResult)) { //find all files from the folder foreach (var item in files) { if (!File.Exists(item)) { exists = false; Logger.LogException(new Exception(item + " not found.")); } } //disconnect connectResult = PInvokeWindowsNetworking.DisconnectRemote(path); if (String.IsNullOrEmpty(connectResult)) { //let's not throw this exception as it does not matter for this service if it cannot disconnect //Logger.LogException(new Exception("Failed to disconnect from " + path + ". Error: " + connectResult)); } } else { Logger.LogException(new Exception("Error connecting to " + path + ". Error: " + connectResult)); } } else { //find all files from the folder foreach (var item in files) { if (!File.Exists(item)) { exists = false; Logger.LogException(new Exception(item + " not found.")); } } } return(exists); }
private List <byte[]> GetImageData(string userName, string password, string path, List <UploadInfo> files) { List <byte[]> imageData = new List <byte[]>(); string connectResult = String.Empty; //connect to share if username and password is required if (!String.IsNullOrEmpty(userName) && !String.IsNullOrEmpty(password)) { connectResult = PInvokeWindowsNetworking.ConnectToRemote(path, userName, password); if (String.IsNullOrEmpty(connectResult)) { foreach (var item in files) { imageData.Add(File.ReadAllBytes(item.FileName)); } //disconnect connectResult = PInvokeWindowsNetworking.DisconnectRemote(path); if (String.IsNullOrEmpty(connectResult)) { //let's not throw this exception as it does not matter for this service if it cannot disconnect //Logger.LogException(new Exception("Failed to disconnect from " + path + ". Error: " + connectResult)); } } else { Logger.LogException(new Exception("Error connecting to " + path + ". Error: " + connectResult)); } } else { foreach (var item in files) { imageData.Add(File.ReadAllBytes(item.FileName)); } } return(imageData); }
private void MoveFiles(string userName, string password, string path, Dictionary <string, string> files) { try { string connectResult = String.Empty; //connect to share if username and password is required if (!String.IsNullOrEmpty(userName) && !String.IsNullOrEmpty(password)) { connectResult = PInvokeWindowsNetworking.ConnectToRemote(path, userName, password); if (String.IsNullOrEmpty(connectResult)) { foreach (var item in files) { Console.WriteLine("Moving " + item.Key + " to " + item.Value); //create directory if required DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(item.Value)); if (!di.Exists) { di.Create(); } if (File.Exists(item.Value)) { File.Delete(item.Value); } File.Move(item.Key, item.Value); } //disconnect connectResult = PInvokeWindowsNetworking.DisconnectRemote(path); if (String.IsNullOrEmpty(connectResult)) { //let's not throw this exception as it does not matter for this service if it cannot disconnect //throw (new FileArchiveException("Failed to disconnect from " + path + ". Error: " + connectResult)); } } else { throw (new FileArchiveException("Error connecting to " + path + ". Error: " + connectResult)); } } else { foreach (var item in files) { Console.WriteLine("Moving " + item.Key + " to " + item.Value); //create directory if required DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(item.Value)); if (!di.Exists) { di.Create(); } if (File.Exists(item.Value)) { File.Delete(item.Value); } File.Move(item.Key, item.Value); } } } catch (Exception ex) { throw new FileArchiveException(ex.ToString()); } }
private void AddSeqToFiles(string userName, string password, string path, string searchPattern) { string[] files = new string[] { }; string connectResult = String.Empty; //connect to share if username and password is required if (!String.IsNullOrEmpty(userName) && !String.IsNullOrEmpty(password)) { connectResult = PInvokeWindowsNetworking.ConnectToRemote(path, userName, password); if (String.IsNullOrEmpty(connectResult)) { //get all files from the folder files = Directory.GetFiles(path, searchPattern, SearchOption.TopDirectoryOnly); //disconnect connectResult = PInvokeWindowsNetworking.DisconnectRemote(path); if (String.IsNullOrEmpty(connectResult)) { //let's not throw this exception as it does not matter for this service if it cannot disconnect //Logger.LogException(new Exception("Failed to disconnect from " + path + ". Error: " + connectResult)); } } else { Logger.LogException(new Exception("Error connecting to " + path + ". Error: " + connectResult)); } } else { //get all files from the folder files = Directory.GetFiles(path, searchPattern, SearchOption.TopDirectoryOnly); } //only keep the files with the same numbe of fileLimits for each load Array.Sort(files); string[] filesForLoad = new string[] { }; string totalPhotosString = String.Empty; processedFiles = Directory.GetFiles(path, loadNumber + "*_processed.jpg", SearchOption.TopDirectoryOnly); processedPhotos = processedFiles.Count(); if (files.Count() > 0) { loadNumber = files[0].Substring(files[0].LastIndexOf('\\') + 1, files[0].IndexOf('_') - (files[0].LastIndexOf('\\') + 1)); //find the complete file and get total photo count. completeFile = Directory.GetFiles(path, loadNumber + "*.ready", SearchOption.TopDirectoryOnly); filesForLoad = Directory.GetFiles(path, loadNumber + "*.jpg", SearchOption.TopDirectoryOnly); if (completeFile.Count() > 0) { totalPhotosString = completeFile[0].Substring(completeFile[0].ToLower().IndexOf("complete_") + 9, completeFile[0].ToLower().IndexOf(".ready") - (completeFile[0].ToLower().IndexOf("complete_") + 9)); if (totalPhotosString.Length > 0) { try { totalPhotos = Int32.Parse(totalPhotosString); } catch (FormatException e) { throw (e); } } } Dictionary <string, int> tempfileOrder = new Dictionary <string, int>(); //add seq to file name for (int i = 0; i < filesForLoad.Count(); i++) { int sequenceNumber = Int32.Parse(GetSequenceNumber(filesForLoad[i].ToLower())); tempfileOrder.Add(filesForLoad[i].ToLower(), sequenceNumber); } //sort fileOrder tempfileOrder = tempfileOrder.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value); int processedIdx = 0; //reduce the gapse in fileOrder for (int index = 0; index < tempfileOrder.Count; index++) { var item = tempfileOrder.ElementAt(index); var itemKey = item.Key; var itemValue = item.Value; if (itemKey.Contains("_processed")) { processedIdx = index + 1; } if (itemValue >= index + 1) { fileOrder.Add(itemKey.ToLower(), (index - processedIdx) + 1); } else { fileOrder.Add(itemKey.ToLower(), itemValue); } } //copy key into a array loadFiles = fileOrder.Keys.ToArray(); } }