sunkaixuan
8 years ago
4 changed files with 88 additions and 2 deletions
@ -0,0 +1,69 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.IO; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
|
||||
|
namespace SqlSugar |
||||
|
{ |
||||
|
public class FileHeper |
||||
|
{ |
||||
|
public static void CreateFile(string filePath, string text, Encoding encoding) |
||||
|
{ |
||||
|
try |
||||
|
{ |
||||
|
if (IsExistFile(filePath)) |
||||
|
{ |
||||
|
DeleteFile(filePath); |
||||
|
} |
||||
|
if (!IsExistFile(filePath)) |
||||
|
{ |
||||
|
string directoryPath = GetDirectoryFromFilePath(filePath); |
||||
|
CreateDirectory(directoryPath); |
||||
|
|
||||
|
//Create File
|
||||
|
FileInfo file = new FileInfo(filePath); |
||||
|
using (FileStream stream = file.Create()) |
||||
|
{ |
||||
|
using (StreamWriter writer = new StreamWriter(stream, encoding)) |
||||
|
{ |
||||
|
writer.Write(text); |
||||
|
writer.Flush(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
catch |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
|
public static bool IsExistDirectory(string directoryPath) |
||||
|
{ |
||||
|
return Directory.Exists(directoryPath); |
||||
|
} |
||||
|
public static void CreateDirectory(string directoryPath) |
||||
|
{ |
||||
|
if (!IsExistDirectory(directoryPath)) |
||||
|
{ |
||||
|
Directory.CreateDirectory(directoryPath); |
||||
|
} |
||||
|
} |
||||
|
public static void DeleteFile(string filePath) |
||||
|
{ |
||||
|
if (IsExistFile(filePath)) |
||||
|
{ |
||||
|
File.Delete(filePath); |
||||
|
} |
||||
|
} |
||||
|
public static string GetDirectoryFromFilePath(string filePath) |
||||
|
{ |
||||
|
FileInfo file = new FileInfo(filePath); |
||||
|
DirectoryInfo directory = file.Directory; |
||||
|
return directory.FullName; |
||||
|
} |
||||
|
public static bool IsExistFile(string filePath) |
||||
|
{ |
||||
|
return File.Exists(filePath); |
||||
|
} |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue