using System;
|
using System.Collections.Generic;
|
using System.IO;
|
using HDL_ON.Entity;
|
|
namespace HDL_ON.Common
|
{
|
public class FileUtlis
|
{
|
static Common.FileUtlis _temp;
|
public static Common.FileUtlis Files
|
{
|
get
|
{
|
if (_temp == null)
|
{
|
_temp = new Common.FileUtlis();
|
}
|
return _temp;
|
}
|
}
|
/// <summary>
|
/// 根目录
|
/// </summary>
|
string RootPath = Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) + "/";
|
|
string accountPath;
|
string AccountPath {
|
get
|
{
|
if (string.IsNullOrEmpty(accountPath) || !accountPath.Contains(UserInfo.Current.ID))
|
{
|
accountPath = Path.Combine(RootPath, UserInfo.Current.CurReginID);
|
if (!Directory.Exists(accountPath))
|
{
|
Directory.CreateDirectory(accountPath);
|
}
|
}
|
return accountPath + "/";
|
}
|
}
|
/// <summary>
|
/// 住宅文件夹路径
|
/// </summary>
|
string regionPath;
|
string RegionPath
|
{
|
get
|
{
|
if (string.IsNullOrEmpty(regionPath) || !regionPath.Contains(UserInfo.Current.CurReginID ))
|
{
|
regionPath = Path.Combine(AccountPath, UserInfo.Current.CurReginID);
|
if (!Directory.Exists(regionPath))
|
{
|
Directory.CreateDirectory(regionPath);
|
}
|
}
|
return regionPath + "/";
|
}
|
}
|
|
|
/// <summary>
|
/// 文件是否存在
|
/// </summary>
|
/// <param name="filePath">File path.</param>
|
public bool Exists(string filePath)
|
{
|
return File.Exists(Path.Combine(Files.RootPath, filePath));
|
}
|
|
/// <summary>
|
/// 遍历文件
|
/// </summary>
|
public List<string> ReadFiles()
|
{
|
List<string> listFiles = new List<string>();
|
DirectoryInfo TheFolder = new DirectoryInfo(RegionPath);
|
foreach (FileInfo NextFile in TheFolder.GetFiles())
|
{
|
listFiles.Add(NextFile.Name);
|
}
|
return listFiles;
|
}
|
|
/// <summary>
|
/// Deletes the file.
|
/// </summary>
|
/// <param name="fileName">File name.</param>
|
public void DeleteFile(string fileName)
|
{
|
File.Delete(Path.Combine(RegionPath, fileName));
|
MainPage.Log("删除文件名为:" + fileName);
|
}
|
|
/// <summary>
|
/// Deletes all file.
|
/// </summary>
|
public void DeleteAllFile()
|
{
|
List<string> filesList = ReadFiles();
|
for (int j = 0; j < filesList.Count; j++)
|
{
|
var f = filesList[j];
|
DeleteFile(f);
|
}
|
}
|
|
/// <summary>
|
/// Writes the file by bytes.
|
/// </summary>
|
/// <returns><c>true</c>, if file by bytes was writed, <c>false</c> otherwise.</returns>
|
/// <param name="fileName">File name.</param>
|
/// <param name="bytes">Bytes.</param>
|
public bool WriteFileByBytes(string fileName, byte[] bytes)
|
{
|
if (fileName == null || (fileName = fileName.Trim()) == (""))
|
{
|
return false;
|
}
|
|
FileStream fs = null;
|
|
try
|
{
|
fs = new FileStream(Path.Combine(RegionPath, fileName), FileMode.Create, FileAccess.Write);
|
fs.Write(bytes, 0, bytes.Length);
|
fs.Flush();
|
MainPage.Log("SaveFile:" + fileName);
|
return true;
|
}
|
catch (Exception ex)
|
{
|
MainPage.Log("FileUtiles Code 113:" + ex.ToString());
|
return false;
|
}
|
finally
|
{
|
try
|
{
|
if (fs != null)
|
{
|
fs.Close();
|
}
|
}
|
catch (Exception ex)
|
{
|
MainPage.Log("FileUtils Code 121 :" + ex.ToString());
|
}
|
}
|
}
|
/// <summary>
|
/// 读取userInfo文件
|
/// </summary>
|
/// <returns></returns>
|
public byte[] ReadUserInfo()
|
{
|
var fileName = "UserInfo_File";
|
FileStream fs = null;
|
try
|
{
|
if (File.Exists(Path.Combine(RootPath, fileName)))
|
{
|
fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Open, FileAccess.Read);
|
}
|
else if (File.Exists(fileName))
|
{
|
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
|
}
|
else
|
{
|
return new byte[0];
|
}
|
byte[] bytes = new byte[fs.Length];
|
fs.Read(bytes, 0, bytes.Length);
|
return bytes;
|
}
|
catch
|
{
|
return new byte[0];
|
}
|
finally
|
{
|
try
|
{
|
if (fs != null)
|
{
|
fs.Close();
|
}
|
}
|
catch
|
{
|
|
}
|
}
|
|
}
|
/// <summary>
|
/// 保存userInfo
|
/// </summary>
|
/// <param name="bytes"></param>
|
/// <returns></returns>
|
public bool WirteUserinfo(byte[] bytes)
|
{
|
var fileName = "UserInfo_File";
|
FileStream fs = null;
|
|
try
|
{
|
fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Create, FileAccess.Write);
|
fs.Write(bytes, 0, bytes.Length);
|
fs.Flush();
|
MainPage.Log("SaveFile:" + fileName);
|
return true;
|
}
|
catch (Exception ex)
|
{
|
MainPage.Log("FileUtiles Code 113:" + ex.ToString());
|
return false;
|
}
|
finally
|
{
|
try
|
{
|
if (fs != null)
|
{
|
fs.Close();
|
}
|
}
|
catch (Exception ex)
|
{
|
MainPage.Log("FileUtils Code 121 :" + ex.ToString());
|
}
|
}
|
}
|
|
/// <summary>
|
/// 读取userInfo文件
|
/// </summary>
|
/// <returns></returns>
|
public byte[] ReadAppConfig()
|
{
|
var fileName = "OnAppConfig";
|
FileStream fs = null;
|
try
|
{
|
if (File.Exists(Path.Combine(AccountPath, fileName)))
|
{
|
fs = new FileStream(Path.Combine(AccountPath, fileName), FileMode.Open, FileAccess.Read);
|
}
|
else if (File.Exists(fileName))
|
{
|
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
|
}
|
else
|
{
|
return new byte[0];
|
}
|
byte[] bytes = new byte[fs.Length];
|
fs.Read(bytes, 0, bytes.Length);
|
return bytes;
|
}
|
catch
|
{
|
return new byte[0];
|
}
|
finally
|
{
|
try
|
{
|
if (fs != null)
|
{
|
fs.Close();
|
}
|
}
|
catch
|
{
|
|
}
|
}
|
|
}
|
/// <summary>
|
/// 保存userInfo
|
/// </summary>
|
/// <param name="bytes"></param>
|
/// <returns></returns>
|
public bool WirteAppConfig(byte[] bytes)
|
{
|
var fileName = "OnAppConfig";
|
FileStream fs = null;
|
|
try
|
{
|
fs = new FileStream(Path.Combine(AccountPath, fileName), FileMode.Create, FileAccess.Write);
|
fs.Write(bytes, 0, bytes.Length);
|
fs.Flush();
|
MainPage.Log("SaveFile:" + fileName);
|
return true;
|
}
|
catch (Exception ex)
|
{
|
MainPage.Log("FileUtiles Code 113:" + ex.ToString());
|
return false;
|
}
|
finally
|
{
|
try
|
{
|
if (fs != null)
|
{
|
fs.Close();
|
}
|
}
|
catch (Exception ex)
|
{
|
MainPage.Log("FileUtils Code 121 :" + ex.ToString());
|
}
|
}
|
}
|
|
|
|
public byte[] ReadFile(string fileName)
|
{
|
FileStream fs = null;
|
try
|
{
|
if (File.Exists(Path.Combine(RegionPath, fileName)))
|
{
|
fs = new FileStream(Path.Combine(RegionPath, fileName), FileMode.Open, FileAccess.Read);
|
}
|
else if (File.Exists(fileName))
|
{
|
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
|
}
|
else
|
{
|
return new byte[0];
|
}
|
byte[] bytes = new byte[fs.Length];
|
fs.Read(bytes, 0, bytes.Length);
|
return bytes;
|
}
|
catch
|
{
|
return new byte[0];
|
}
|
finally
|
{
|
try
|
{
|
if (fs != null)
|
{
|
fs.Close();
|
}
|
}
|
catch
|
{
|
|
}
|
}
|
}
|
}
|
}
|