using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
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;
}
}
///
/// 根目录
///
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.ID);
if (!Directory.Exists(accountPath))
{
Directory.CreateDirectory(accountPath);
}
}
return accountPath + "/";
}
}
///
/// 住宅文件夹路径
///
string regionPath;
string RegionPath
{
get
{
if (DB_ResidenceData.Instance.CurrentRegion == null || DB_ResidenceData.Instance.CurrentRegion.id == null)
{
return AccountPath;
}
if (string.IsNullOrEmpty(regionPath) || !regionPath.Contains(DB_ResidenceData.Instance.CurrentRegion.id))
{
regionPath = Path.Combine(AccountPath, DB_ResidenceData.Instance.CurrentRegion.id);
if (!Directory.Exists(regionPath))
{
Directory.CreateDirectory(regionPath);
}
}
return regionPath + "/";
}
}
//public List<>
///
/// 遍历文件
///
public List ReadFiles()
{
List listFiles = new List();
DirectoryInfo TheFolder = new DirectoryInfo(RegionPath);
foreach (FileInfo NextFile in TheFolder.GetFiles())
{
listFiles.Add(NextFile.Name);
}
return listFiles;
}
///
/// Deletes the file.
///
/// File name.
public void DeleteFile(string fileName)
{
File.Delete(Path.Combine(RegionPath, fileName));
MainPage.Log("删除文件名为:" + fileName);
}
///
/// Deletes all file.
///
public void DeleteAllFile()
{
List filesList = ReadFiles();
for (int j = 0; j < filesList.Count; j++)
{
var f = filesList[j];
DeleteFile(f);
}
}
///
/// Writes the file by bytes.
///
/// true, if file by bytes was writed, false otherwise.
/// File name.
/// Bytes.
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}");//Path:{RegionPath}
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());
}
}
}
///
/// 读取userInfo文件
///
///
public byte[] ReadAppConfig()
{
var fileName = "OnAppConfig";
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
{
}
}
}
///
/// 保存userInfo
///
///
///
public bool WirteAppConfig(byte[] bytes)
{
var fileName = "OnAppConfig";
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());
}
}
}
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
{
}
}
}
// 读取指定路径文件内容
public byte[] ReadFileForPath(string path)
{
FileStream fs = null;
try
{
if (File.Exists(path))
{
fs = new FileStream(path, 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
{
}
}
}
///
/// 下载网络图片
///
///
///
public void DownLoadImage(string fileName, string url, Action action)
{
if (!File.Exists(fileName))
{
System.Threading.Tasks.Task.Run(() =>
{
FileStream fs = null;
try
{
MyWebClient webClient = new MyWebClient();
byte[] recevieBytes = webClient.DownloadData(new Uri(url));
fs = new FileStream(Path.Combine(RootPath, fileName), FileMode.Create, FileAccess.Write);
//fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
fs.Write(recevieBytes, 0, recevieBytes.Length);
fs.Flush();
}
catch (Exception ex)
{
MainPage.Log($"down image : {ex.Message}");
}
finally
{
if (fs != null)
{
fs.Close();
action?.Invoke();
}
}
});
}
else
{
action?.Invoke();
}
}
}
public class MyWebClient : WebClient
{
int _timeout = 10000;
///
/// 超时时间(毫秒)
///
public int Timeout
{
get
{
return _timeout;
}
set
{
_timeout = value;
}
}
public MyWebClient()
{
}
public MyWebClient(int timeout)
{
this._timeout = timeout;
}
protected override WebRequest GetWebRequest(Uri address)
{
var result = base.GetWebRequest(address);
result.Timeout = this._timeout;
return result;
}
}
}