mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-05-26 02:34:05 +02:00
Add WindowsUtils.CreateDirectoryForFile and use it
This commit is contained in:
parent
441228e2b0
commit
fe3fc5c9f7
Configuration
Core/Utils
Data
Plugins
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using TweetDuck.Core.Utils;
|
||||||
|
|
||||||
namespace TweetDuck.Configuration{
|
namespace TweetDuck.Configuration{
|
||||||
sealed class SystemConfig{
|
sealed class SystemConfig{
|
||||||
@ -31,10 +32,7 @@ private void ReadFromStream(Stream stream){
|
|||||||
|
|
||||||
public bool Save(){
|
public bool Save(){
|
||||||
try{
|
try{
|
||||||
string directory = Path.GetDirectoryName(file);
|
WindowsUtils.CreateDirectoryForFile(file);
|
||||||
if (directory == null)return false;
|
|
||||||
|
|
||||||
Directory.CreateDirectory(directory);
|
|
||||||
|
|
||||||
using(Stream stream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None)){
|
using(Stream stream = new FileStream(file, FileMode.Create, FileAccess.Write, FileShare.None)){
|
||||||
WriteToStream(stream);
|
WriteToStream(stream);
|
||||||
|
@ -139,10 +139,7 @@ bool ISerializedObject.OnReadUnknownProperty(string property, string value){
|
|||||||
|
|
||||||
public bool Save(){
|
public bool Save(){
|
||||||
try{
|
try{
|
||||||
string directory = Path.GetDirectoryName(file);
|
WindowsUtils.CreateDirectoryForFile(file);
|
||||||
if (directory == null)return false;
|
|
||||||
|
|
||||||
Directory.CreateDirectory(directory);
|
|
||||||
|
|
||||||
if (File.Exists(file)){
|
if (File.Exists(file)){
|
||||||
string backupFile = GetBackupFile(file);
|
string backupFile = GetBackupFile(file);
|
||||||
|
@ -24,6 +24,17 @@ static WindowsUtils(){
|
|||||||
ShouldAvoidToolWindow = ver.Major == 6 && ver.Minor == 2; // windows 8/10
|
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){
|
public static bool CheckFolderWritePermission(string path){
|
||||||
string testFile = Path.Combine(path, ".test");
|
string testFile = Path.Combine(path, ".test");
|
||||||
|
|
||||||
|
@ -120,11 +120,7 @@ public void WriteToFile(string path){
|
|||||||
|
|
||||||
public void WriteToFile(string path, bool createDirectory){
|
public void WriteToFile(string path, bool createDirectory){
|
||||||
if (createDirectory){
|
if (createDirectory){
|
||||||
string dir = Path.GetDirectoryName(path);
|
WindowsUtils.CreateDirectoryForFile(path);
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(dir)){
|
|
||||||
Directory.CreateDirectory(dir);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
File.WriteAllBytes(path, contents);
|
File.WriteAllBytes(path, contents);
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using TweetDuck.Core.Utils;
|
||||||
using TweetDuck.Data;
|
using TweetDuck.Data;
|
||||||
using TweetDuck.Plugins.Enums;
|
using TweetDuck.Plugins.Enums;
|
||||||
using TweetDuck.Plugins.Events;
|
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){
|
public void WriteFile(int token, string path, string contents){
|
||||||
string fullPath = GetFullPathOrThrow(token, PluginFolder.Data, path);
|
string fullPath = GetFullPathOrThrow(token, PluginFolder.Data, path);
|
||||||
|
|
||||||
// ReSharper disable once AssignNullToNotNullAttribute
|
WindowsUtils.CreateDirectoryForFile(fullPath);
|
||||||
Directory.CreateDirectory(Path.GetDirectoryName(fullPath));
|
|
||||||
|
|
||||||
File.WriteAllText(fullPath, contents, Encoding.UTF8);
|
File.WriteAllText(fullPath, contents, Encoding.UTF8);
|
||||||
fileCache[token, SanitizeCacheKey(path)] = contents;
|
fileCache[token, SanitizeCacheKey(path)] = contents;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user