/// <summary> /// 지정 경로의 파일 검색 /// </summary> /// <param name="directoryPath">검색 대상 폴더 경로 (생략할 경우 directoryPath Property 사용)</param> /// <param name="searchPattern">검색 패턴 (생략할 경우 전체 검색)</param> /// <returns>함수 실행 결과 (FileResult 객체)</returns> public CommonStruct.FileResult SearchFile(string directoryPath = null, string searchPattern = "*.*") { CommonStruct.FileResult result = new CommonStruct.FileResult(); bool isSuccess = false; Exception funcException = null; FileInfo[] resultFiles = null; string resultString = null; string pDirectoryPath = string.Empty; stopWatch.Start(); try { if (directoryPath == null) { pDirectoryPath = this.directoryPath; } else { pDirectoryPath = directoryPath; } if (Directory.Exists(pDirectoryPath)) { DirectoryInfo directoryInfo = new DirectoryInfo(pDirectoryPath); isSuccess = true; resultFiles = directoryInfo.GetFiles(searchPattern); } else { isSuccess = false; resultFiles = null; } funcException = null; resultString = null; } catch (Exception ex) { isSuccess = false; funcException = ex; resultString = null; } stopWatch.Stop(); result.funcResult.isSuccess = isSuccess; result.funcResult.funcException = funcException; result.funcResult.totalMilliseconds = stopWatch.ElapsedMilliseconds; result.resultFiles = resultFiles; result.resultString = resultString; stopWatch.Reset(); return(result); }
/// <summary> /// 지정 FTP 경로의 파일 읽기 /// </summary> /// <param name="remotePath">원격 파일 경로</param> /// <returns>함수 실행 결과 (FileResult 객체)</returns> public CommonStruct.FileResult GetFileData(string remotePath) { CommonStruct.FileResult result = new CommonStruct.FileResult(); Exception funcException = null; string resultString = string.Empty; bool isSuccess = false; WebClient ftpClient = new WebClient(); stopWatch.Start(); try { if (ftpUserId.Equals(string.Empty) || ftpUserPw.Equals(string.Empty)) { resultString = string.Empty; isSuccess = false; } else { if (ftpClient.Credentials == null) { ftpClient.Credentials = ftpUserInfo; } resultString = Encoding.UTF8.GetString(ftpClient.DownloadData(remotePath)); isSuccess = true; } funcException = null; } catch (Exception ex) { resultString = string.Empty; isSuccess = false; funcException = ex; } stopWatch.Stop(); result.funcResult.isSuccess = isSuccess; result.funcResult.funcException = funcException; result.funcResult.totalMilliseconds = stopWatch.ElapsedMilliseconds; result.resultFiles = null; result.resultString = resultString; stopWatch.Reset(); return(result); }
/// <summary> /// Initialize 파일의 설정 값 읽기 /// </summary> /// <param name="iniPath">Initialize 파일 경로</param> /// <param name="section">Initialize Section</param> /// <param name="key">Initialize Key</param> /// <returns>함수 실행 결과 (FileResult 객체)</returns> public static CommonStruct.FileResult GetConfig(string iniPath, string section, string key) { CommonStruct.FileResult result = new CommonStruct.FileResult(); Exception funcException = null; string resultString = string.Empty; bool isSuccess = false; StringBuilder temp = new StringBuilder(255); stopWatch.Start(); try { GetPrivateProfileString(section, key, string.Empty, temp, 255, iniPath); resultString = temp.ToString(); isSuccess = true; funcException = null; } catch (Exception ex) { resultString = string.Empty; isSuccess = false; funcException = ex; } stopWatch.Stop(); result.funcResult.isSuccess = isSuccess; result.funcResult.funcException = funcException; result.funcResult.totalMilliseconds = stopWatch.ElapsedMilliseconds; result.resultFiles = null; result.resultString = resultString; stopWatch.Reset(); return(result); }
/// <summary> /// 저장 일자 기준으로 저장된 로그 파일을 삭제 /// </summary> /// <param name="logDirectoryPath">로그 파일 저장 폴더 경로</param> /// <param name="logSaveDay">로그 파일 저장 일</param> public static void DeleteLog(string logDirectoryPath, int logSaveDay) { try { if (Directory.Exists(logDirectoryPath)) { CommonStruct.FileResult result = new CommonStruct.FileResult(); DateTime deleteDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day).AddDays(logSaveDay * (-1)); FileHandler fileHandle = new FileHandler(); result = fileHandle.SearchFile(logDirectoryPath, "*.txt"); FileInfo[] deleteFiles = result.resultFiles.Where(p => p.CreationTime < deleteDate).ToArray(); foreach (FileInfo f in deleteFiles) { f.Delete(); } } } catch (Exception ex) { } }
/// <summary> /// 지정 경로의 파일 읽기 /// </summary> /// <param name="filePath">읽을 파일 경로 (생략할 경우 filePath Property 사용)</param> /// <param name="readLines">읽을 Line 수 (생략할 경우 전체 내용 읽기)</param> /// <returns>함수 실행 결과 (FileResult 객체)</returns> public CommonStruct.FileResult ReadFile(string filePath = null, ushort readLines = 0) { CommonStruct.FileResult result = new CommonStruct.FileResult(); StreamReader fileReader = null; FileStream fileStream = null; string fileContents = string.Empty; stopWatch.Start(); try { if (filePath == null) { if (this.filePath.Equals(string.Empty)) { fileContents = string.Empty; } } else { this.filePath = filePath; } if (isExist) { fileStream = new FileStream(this.filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); fileReader = new StreamReader(fileStream); if (readLines == 0) { fileContents = fileReader.ReadToEnd(); fileContents = fileContents.Replace("\r\n", "\n"); } else { for (int i = 0; i < readLines; i++) { string currentLine = fileReader.ReadLine(); if (currentLine != null) { currentLine = currentLine.Replace("\r\n", "\n"); fileContents += string.Format("{0}\n", currentLine); } else { break; } } } result.funcResult.isSuccess = true; } else { fileContents = string.Empty; result.funcResult.isSuccess = false; } result.funcResult.funcException = null; } catch (Exception ex) { //LogHandler.WriteLog(string.Empty, string.Format("{0} :: FileRead() Exception :: Message = {1}", this.ToString(), ex.Message)); fileContents = string.Empty; result.funcResult.isSuccess = false; result.funcResult.funcException = ex; } finally { if (fileStream != null) { fileStream.Close(); } if (fileReader != null) { fileReader.Close(); } } stopWatch.Stop(); result.funcResult.totalMilliseconds = stopWatch.ElapsedMilliseconds; result.resultFiles = null; result.resultString = fileContents; stopWatch.Reset(); return(result); }