1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-05 20:34:07 +02:00

Add WindowsUtils.CreateDirectoryForFile and use it

This commit is contained in:
chylex 2017-07-09 14:12:27 +02:00
parent 441228e2b0
commit fe3fc5c9f7
5 changed files with 17 additions and 16 deletions

View File

@ -1,5 +1,6 @@
using System;
using System.IO;
using TweetDuck.Core.Utils;
namespace TweetDuck.Configuration{
sealed class SystemConfig{
@ -31,10 +32,7 @@ private void ReadFromStream(Stream stream){
public bool Save(){
try{
string directory = Path.GetDirectoryName(file);
if (directory == null)return false;
Directory.CreateDirectory(directory);
WindowsUtils.CreateDirectoryForFile(file);
using(Stream stream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None)){
WriteToStream(stream);

View File

@ -139,10 +139,7 @@ bool ISerializedObject.OnReadUnknownProperty(string property, string value){
public bool Save(){
try{
string directory = Path.GetDirectoryName(file);
if (directory == null)return false;
Directory.CreateDirectory(directory);
WindowsUtils.CreateDirectoryForFile(file);
if (File.Exists(file)){
string backupFile = GetBackupFile(file);

View File

@ -24,6 +24,17 @@ static WindowsUtils(){
ShouldAvoidToolWindow = ver.Major == 6 && ver.Minor == 2; // windows 8/10
}
public static void CreateDirectoryForFile(string file){
string dir = Path.GetDirectoryName(file);
if (dir == null){
throw new ArgumentException("Invalid file path: "+file);
}
else if (dir.Length > 0){
Directory.CreateDirectory(dir);
}
}
public static bool CheckFolderWritePermission(string path){
string testFile = Path.Combine(path, ".test");

View File

@ -120,11 +120,7 @@ public void WriteToFile(string path){
public void WriteToFile(string path, bool createDirectory){
if (createDirectory){
string dir = Path.GetDirectoryName(path);
if (!string.IsNullOrEmpty(dir)){
Directory.CreateDirectory(dir);
}
WindowsUtils.CreateDirectoryForFile(path);
}
File.WriteAllBytes(path, contents);

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using TweetDuck.Core.Utils;
using TweetDuck.Data;
using TweetDuck.Plugins.Enums;
using TweetDuck.Plugins.Events;
@ -78,9 +79,7 @@ private string ReadFileUnsafe(int token, string cacheKey, string fullPath, bool
public void WriteFile(int token, string path, string contents){
string fullPath = GetFullPathOrThrow(token, PluginFolder.Data, path);
// ReSharper disable once AssignNullToNotNullAttribute
Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
WindowsUtils.CreateDirectoryForFile(fullPath);
File.WriteAllText(fullPath, contents, Encoding.UTF8);
fileCache[token, SanitizeCacheKey(path)] = contents;
}