public static List <string> GetAllFileInFodler(string folder) { return(new List <string>( from original in SafeDirectory.GetFiles(folder, "*.*", SearchOption.AllDirectories) select original.Remove(0, (folder + Path.DirectorySeparatorChar).Length) )); }
public static void CopyDirectory(string src, string dst) { String[] files; if (dst[dst.Length - 1] != Path.DirectorySeparatorChar) { dst += Path.DirectorySeparatorChar; } if (!SafeDirectory.Exists(dst)) { SafeDirectory.CreateDirectory(dst); } files = SafeDirectory.GetFileSystemEntries(src); foreach (string element in files) { // Sub directories if (SafeDirectory.Exists(element)) { CopyDirectory(element, dst + Path.GetFileName(element)); } // Files in directory else { SafeFile.Copy(element, dst + Path.GetFileName(element), true); } } }
public static void CopyDirectory(string Src, string Dst) { String[] Files; if (Dst[Dst.Length - 1] != Path.DirectorySeparatorChar) { Dst += Path.DirectorySeparatorChar; } if (!SafeDirectory.Exists(Dst)) { SafeDirectory.CreateDirectory(Dst); } Files = SafeDirectory.GetFileSystemEntries(Src); foreach (string Element in Files) { // Sub directories if (SafeDirectory.Exists(Element)) { CopyDirectory(Element, Dst + Path.GetFileName(Element)); } // Files in directory else { SafeFile.Copy(Element, Dst + Path.GetFileName(Element), true); } } }
public static void ExtractZipFile(string archiveFilenameIn, string password, string outFolder) { ZipFile zf = null; try { FileStream fs = SafeFile.OpenRead(archiveFilenameIn); zf = new ZipFile(fs); if (!String.IsNullOrEmpty(password)) { zf.Password = password; // AES encrypted entries are handled automatically } foreach (ZipEntry zipEntry in zf) { if (!zipEntry.IsFile) { continue; // Ignore directories } String entryFileName = zipEntry.Name; // to remove the folder from the entry:- entryFileName = Path.GetFileName(entryFileName); // Optionally match entrynames against a selection list here to skip as desired. // The unpacked length is available in the zipEntry.Size property. byte[] buffer = new byte[4096]; // 4K is optimum Stream zipStream = zf.GetInputStream(zipEntry); // Manipulate the output filename here as desired. String fullZipToPath = Path.Combine(outFolder, entryFileName); string directoryName = Path.GetDirectoryName(fullZipToPath); if (directoryName.Length > 0) { SafeDirectory.CreateDirectory(directoryName); } // Unzip file in buffered chunks. This is just as fast as unpacking to a buffer the full size // of the file, but does not waste memory. // The "using" will close the stream even if an exception occurs. using (FileStream streamWriter = SafeFile.Create(fullZipToPath)) { StreamUtils.Copy(zipStream, streamWriter, buffer); } } } finally { if (zf != null) { zf.IsStreamOwner = true; // Makes close also shut the underlying stream zf.Close(); // Ensure we release resources } } }
private static IEnumerable <ProcessStartInfo> ParseResource(string resourceName) { var lines = ScriptResource.ResourceManager.GetString(resourceName); foreach (var line in lines.Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)) { var exePath = line.Substring(0, line.IndexOf(' ')).Trim(); var args = line.Substring(exePath.Length).Trim(); yield return(new ProcessStartInfo { FileName = exePath, Arguments = args, WindowStyle = ProcessWindowStyle.Hidden, WorkingDirectory = SafeDirectory.GetCurrentDirectory(), }); } }
public static void RecreateDirectory(string dirPath) { if (SafeDirectory.Exists(dirPath)) { try { SafeDirectory.Delete(dirPath, true); } catch (IOException ex) { LogUtil.LogException(ex); if (SafeDirectory.EnumerateFiles(dirPath).Any()) { throw; } return; // Can't delete dir, but it is empty => skip it } } SafeDirectory.CreateDirectory(dirPath); }
public static void CleanUp() { SafeDirectory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory); LogUtil.LogEvent("clean up"); try { foreach (var name in GetProcessesToKill()) { foreach (var process in Process.GetProcessesByName(name)) { LogUtil.LogEvent(string.Format("{0} process kill", name)); try { process.Kill(); process.WaitForExit(1000); // wait for exit no longer than 1 second } catch (Win32Exception) { } catch (InvalidOperationException) { } } } if (SafeDirectory.Exists(WORKING_FOLDER)) { SafeDirectory.Delete(WORKING_FOLDER, true); } } catch (Exception ex) { LogUtil.LogException(ex); // Do nothing } }