CreateDirectoryFromFilePath

and CreateDirectoryFromDirectoryPath
This commit is contained in:
Michael Delpach 2016-02-22 07:15:23 +08:00
parent 8d34f380ff
commit 0f17aeeec2
15 changed files with 36 additions and 36 deletions

View file

@ -54,7 +54,7 @@ public void Encode(string sourceFilePath, string targetFilePath)
targetFilePath += "." + OutputExtension.TrimStart('.'); targetFilePath += "." + OutputExtension.TrimStart('.');
} }
Helpers.CreateDirectoryIfNotExist(targetFilePath); Helpers.CreateDirectoryFromFilePath(targetFilePath);
using (Process process = new Process()) using (Process process = new Process())
{ {

View file

@ -107,7 +107,7 @@ public static bool WriteToFile(this Stream stream, string filePath)
{ {
if (stream.Length > 0 && !string.IsNullOrEmpty(filePath)) if (stream.Length > 0 && !string.IsNullOrEmpty(filePath))
{ {
Helpers.CreateDirectoryIfNotExist(filePath); Helpers.CreateDirectoryFromFilePath(filePath);
using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read)) using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read))
{ {

View file

@ -69,7 +69,7 @@ public static bool ExtractFFmpeg(string zipPath, string extractPath)
SevenZipExtractor.SetLibraryPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "7z.dll")); SevenZipExtractor.SetLibraryPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "7z.dll"));
} }
Helpers.CreateDirectoryIfNotExist(extractPath); Helpers.CreateDirectoryFromFilePath(extractPath);
using (SevenZipExtractor zip = new SevenZipExtractor(zipPath)) using (SevenZipExtractor zip = new SevenZipExtractor(zipPath))
{ {

View file

@ -732,15 +732,8 @@ public static bool IsFileLocked(string path)
return false; return false;
} }
public static void CreateDirectoryIfNotExist(string path, bool isFilePath = true) public static void CreateDirectoryFromDirectoryPath(string path)
{ {
if (!string.IsNullOrEmpty(path))
{
if (isFilePath)
{
path = Path.GetDirectoryName(path);
}
if (!string.IsNullOrEmpty(path) && !Directory.Exists(path)) if (!string.IsNullOrEmpty(path) && !Directory.Exists(path))
{ {
try try
@ -754,6 +747,13 @@ public static void CreateDirectoryIfNotExist(string path, bool isFilePath = true
} }
} }
} }
public static void CreateDirectoryFromFilePath(string path)
{
if (!string.IsNullOrEmpty(path))
{
CreateDirectoryFromDirectoryPath(Path.GetDirectoryName(path));
}
} }
public static void BackupFileMonthly(string filepath, string destinationFolder) public static void BackupFileMonthly(string filepath, string destinationFolder)
@ -767,7 +767,7 @@ public static void BackupFileMonthly(string filepath, string destinationFolder)
if (!File.Exists(newFilepath)) if (!File.Exists(newFilepath))
{ {
CreateDirectoryIfNotExist(newFilepath); CreateDirectoryFromFilePath(newFilepath);
File.Copy(filepath, newFilepath, false); File.Copy(filepath, newFilepath, false);
} }
} }
@ -785,7 +785,7 @@ public static void BackupFileWeekly(string filepath, string destinationFolder)
if (!File.Exists(newFilepath)) if (!File.Exists(newFilepath))
{ {
CreateDirectoryIfNotExist(newFilepath); CreateDirectoryFromFilePath(newFilepath);
File.Copy(filepath, newFilepath, false); File.Copy(filepath, newFilepath, false);
} }
} }

View file

@ -53,7 +53,7 @@ public Logger(string logFilePath)
{ {
FileWrite = true; FileWrite = true;
LogFilePath = logFilePath; LogFilePath = logFilePath;
Helpers.CreateDirectoryIfNotExist(LogFilePath); Helpers.CreateDirectoryFromFilePath(LogFilePath);
} }
protected void OnMessageAdded(string message) protected void OnMessageAdded(string message)

View file

@ -121,7 +121,7 @@ private static bool SaveInternal(object obj, string filePath, bool createBackup)
{ {
lock (obj) lock (obj)
{ {
Helpers.CreateDirectoryIfNotExist(filePath); Helpers.CreateDirectoryFromFilePath(filePath);
string tempFilePath = filePath + ".temp"; string tempFilePath = filePath + ".temp";

View file

@ -93,7 +93,7 @@ public bool Append(params HistoryItem[] historyItems)
{ {
lock (thisLock) lock (thisLock)
{ {
Helpers.CreateDirectoryIfNotExist(FilePath); Helpers.CreateDirectoryFromFilePath(FilePath);
using (FileStream fs = File.Open(FilePath, FileMode.Append, FileAccess.Write, FileShare.Read)) using (FileStream fs = File.Open(FilePath, FileMode.Append, FileAccess.Write, FileShare.Read))
using (XmlTextWriter writer = new XmlTextWriter(fs, Encoding.UTF8)) using (XmlTextWriter writer = new XmlTextWriter(fs, Encoding.UTF8))

View file

@ -44,7 +44,7 @@ public FFmpegCLIManager(string ffmpegPath)
Output = new StringBuilder(); Output = new StringBuilder();
OutputDataReceived += FFmpeg_DataReceived; OutputDataReceived += FFmpeg_DataReceived;
ErrorDataReceived += FFmpeg_DataReceived; ErrorDataReceived += FFmpeg_DataReceived;
Helpers.CreateDirectoryIfNotExist(FFmpegPath); Helpers.CreateDirectoryFromFilePath(FFmpegPath);
} }
private void FFmpeg_DataReceived(object sender, DataReceivedEventArgs e) private void FFmpeg_DataReceived(object sender, DataReceivedEventArgs e)

View file

@ -162,7 +162,7 @@ private string GetOutputDirectory()
break; break;
} }
Helpers.CreateDirectoryIfNotExist(directory, false); Helpers.CreateDirectoryFromDirectoryPath(directory);
return directory; return directory;
} }

View file

@ -58,7 +58,7 @@ public FFmpegHelper(ScreencastOptions options)
OutputDataReceived += FFmpegHelper_DataReceived; OutputDataReceived += FFmpegHelper_DataReceived;
ErrorDataReceived += FFmpegHelper_DataReceived; ErrorDataReceived += FFmpegHelper_DataReceived;
Options = options; Options = options;
Helpers.CreateDirectoryIfNotExist(Options.OutputPath); Helpers.CreateDirectoryFromFilePath(Options.OutputPath);
} }
private void FFmpegHelper_DataReceived(object sender, DataReceivedEventArgs e) private void FFmpegHelper_DataReceived(object sender, DataReceivedEventArgs e)

View file

@ -52,7 +52,7 @@ public int Count
public HardDiskCache(ScreencastOptions options) public HardDiskCache(ScreencastOptions options)
{ {
Options = options; Options = options;
Helpers.CreateDirectoryIfNotExist(Options.OutputPath); Helpers.CreateDirectoryFromFilePath(Options.OutputPath);
fsCache = new FileStream(Options.OutputPath, FileMode.Create, FileAccess.Write, FileShare.Read); fsCache = new FileStream(Options.OutputPath, FileMode.Create, FileAccess.Write, FileShare.Read);
indexList = new List<LocationInfo>(); indexList = new List<LocationInfo>();
} }

View file

@ -204,7 +204,7 @@ public void SaveAsGIF(string path, GIFQuality quality)
{ {
if (imgCache != null && imgCache is HardDiskCache && !IsRecording) if (imgCache != null && imgCache is HardDiskCache && !IsRecording)
{ {
Helpers.CreateDirectoryIfNotExist(path); Helpers.CreateDirectoryFromFilePath(path);
HardDiskCache hdCache = imgCache as HardDiskCache; HardDiskCache hdCache = imgCache as HardDiskCache;
@ -229,7 +229,7 @@ public void SaveAsGIF(string path, GIFQuality quality)
public bool FFmpegEncodeAsGIF(string path) public bool FFmpegEncodeAsGIF(string path)
{ {
Helpers.CreateDirectoryIfNotExist(path); Helpers.CreateDirectoryFromFilePath(path);
return ffmpegCli.EncodeGIF(Options.OutputPath, path); return ffmpegCli.EncodeGIF(Options.OutputPath, path);
} }

View file

@ -43,7 +43,7 @@ public override UploadResult Upload(Stream stream, string fileName)
string filePath = account.GetLocalhostPath(fileName); string filePath = account.GetLocalhostPath(fileName);
Helpers.CreateDirectoryIfNotExist(filePath); Helpers.CreateDirectoryFromFilePath(filePath);
using (FileStream fs = new FileStream(filePath, FileMode.Create)) using (FileStream fs = new FileStream(filePath, FileMode.Create))
{ {

View file

@ -42,7 +42,7 @@ public ChromeForm()
private void CreateChromeHostManifest(string filepath) private void CreateChromeHostManifest(string filepath)
{ {
Helpers.CreateDirectoryIfNotExist(filepath); Helpers.CreateDirectoryFromFilePath(filepath);
var manifest = new var manifest = new
{ {

View file

@ -672,7 +672,7 @@ private void DoTextJobs()
if (!string.IsNullOrEmpty(filePath)) if (!string.IsNullOrEmpty(filePath))
{ {
Info.FilePath = filePath; Info.FilePath = filePath;
Helpers.CreateDirectoryIfNotExist(Info.FilePath); Helpers.CreateDirectoryFromFilePath(Info.FilePath);
File.WriteAllText(Info.FilePath, tempText, Encoding.UTF8); File.WriteAllText(Info.FilePath, tempText, Encoding.UTF8);
DebugHelper.WriteLine("Text saved to file: " + Info.FilePath); DebugHelper.WriteLine("Text saved to file: " + Info.FilePath);
} }
@ -1361,7 +1361,7 @@ private bool DownloadAndUpload()
try try
{ {
Helpers.CreateDirectoryIfNotExist(Info.FilePath); Helpers.CreateDirectoryFromFilePath(Info.FilePath);
using (WebClient wc = new WebClient()) using (WebClient wc = new WebClient())
{ {