static void SetProjectBasePath(string modulePath) { modulePath = PathTool.NormalizePath(modulePath); DirectoryInfo dirInfo = new DirectoryInfo(modulePath); while (dirInfo != null) { var files = dirInfo.GetFiles(PROJECT_ROOT_MARK_FILE); if (files != null && files.Length >= 1) { break; } dirInfo = dirInfo.Parent; } if (dirInfo == null) { return; } ConfigSetting.SetValue("PROJECT_BASE", dirInfo.FullName); }
public static string CombinePath(string path, string filename) { if (string.IsNullOrEmpty(path)) { return(filename); } if (string.IsNullOrEmpty(filename)) { return(path); } path = PathTool.NormalizePath(path); var normalizedFilename = PathTool.NormalizePath(filename); if (!normalizedFilename.ToLower().StartsWith(path.ToLower())) { return(Path.Combine(path, normalizedFilename)); } else { return(normalizedFilename); } }
public static void CopyFiles(string srcPath, string destPath, bool isRecursive = true) { srcPath = PathTool.NormalizePath(srcPath); destPath = PathTool.NormalizePathAndCreate(destPath); var srcDir = Path.GetDirectoryName(srcPath); var srcPattern = Path.GetFileName(srcPath); var destFileName = Path.GetFileName(destPath); var srcFiles = Directory.GetFiles(srcDir, srcPattern); foreach (var srcFile in srcFiles) { var srcFileInfo = new FileInfo(srcFile); if (string.IsNullOrEmpty(destFileName)) { srcFileInfo.CopyTo(Path.Combine(destPath, Path.GetFileName(srcFile)), true); } else { // if dest path is a file just copy first one srcFileInfo.CopyTo(destPath, true); return; } } if (isRecursive) { var subDirs = Directory.GetDirectories(Path.GetDirectoryName(srcPath)); foreach (var subDir in subDirs) { var dirName = subDir.Substring(srcDir.Length + 1); var subsrcPath = Path.Combine(subDir, srcPattern); var subdestPath = Path.Combine(destPath, dirName) + Path.DirectorySeparatorChar; CopyFiles(subsrcPath, subdestPath); } } }
static public void WriteIfChanged(string strTargetPath, byte[] bufferDataToWrite, long length, bool forceWrite = false) { strTargetPath = PathTool.NormalizePath(strTargetPath); PathTool.NormalizePathAndCreate(Path.GetFullPath(strTargetPath)); //string targetDir = Path.GetDirectoryName(strTargetPath); string targetFileName = Path.GetFileName(strTargetPath); var sourceFileBuffer = new byte[7 * 1024]; int sourceFileReadSize = 0; var targetFileBuffer = new byte[7 * 1024]; int targetFileReadSize = 0; bool bIsMissMatched = false; try { using (var source = new FileStream(strTargetPath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (var memoryFile = new MemoryStream(bufferDataToWrite, 0, (int)length, false)) { if (source.Length != length) { bIsMissMatched = true; } while (!bIsMissMatched) { sourceFileReadSize = source.Read(sourceFileBuffer, 0, sourceFileBuffer.Length); targetFileReadSize = memoryFile.Read(targetFileBuffer, 0, targetFileBuffer.Length); if (targetFileReadSize != sourceFileReadSize) { bIsMissMatched = true; break; } else if (targetFileReadSize == 0) { break; } int iChar = 0; for (iChar = 0; iChar < sourceFileReadSize; iChar++) { if (sourceFileBuffer[iChar] != targetFileBuffer[iChar]) { bIsMissMatched = true; break; } } } } } catch (Exception exp) { System.Diagnostics.Debug.WriteLine(exp.Message); // maybe file not exist, create new one bIsMissMatched = true; } if (bIsMissMatched) { Console.WriteLine("Writing : " + targetFileName); using (var source = new FileStream(strTargetPath, FileMode.Create, FileAccess.Write, FileShare.None)) { source.Write(bufferDataToWrite, 0, (int)length); } } else { //Console.WriteLine("All updated. Skipping : " + targetFileName); } }
/// <summary> /// Get path setting /// </summary> /// <param name="keyBasePath"></param> /// <param name="keyFileName"></param> /// <returns></returns> public string GetPath(string keyBasePath, string keyFileName) { return(PathTool.CombinePath(GetValue <string>(keyBasePath), GetValue <string>(keyFileName))); }