using System;
|
using System.IO;
|
using System.Collections.Generic;
|
|
namespace Shared.IO
|
{
|
public static class FileUtils
|
{
|
|
public static string RootPath = Environment.GetFolderPath (System.Environment.SpecialFolder.Personal) + "/";
|
|
/// <summary>
|
/// 创建一个住宅的备份文件夹
|
/// </summary>
|
public static string CreateRegionBackup (string regionName)
|
{
|
var path = Path.Combine (RootPath, regionName);
|
if (!Directory.Exists (path)) {
|
Directory.CreateDirectory (path);
|
}
|
return path + "/";
|
}
|
|
/// <summary>
|
/// 删除指定区域里面的所有文件
|
/// </summary>
|
/// <param name="path">Path.</param>
|
public static void DeleteRegionFiles(string path)
|
{
|
foreach (var fileName in ReadRegionFiles (path)) {
|
System.IO.File.Delete (System.IO.Path.Combine (path, fileName));
|
Console.WriteLine ("Del region file :" + fileName);
|
}
|
}
|
|
/// <summary>
|
/// 读取区域文件夹里面的文件
|
/// </summary>
|
public static List<string> ReadRegionFiles (string regionRootPath)
|
{
|
List<string> listFiles = new List<string> ();
|
DirectoryInfo TheFolder = new DirectoryInfo (regionRootPath);
|
foreach (FileInfo NextFile in TheFolder.GetFiles ()) {
|
listFiles.Add (NextFile.Name);
|
}
|
return listFiles;
|
}
|
|
/// <summary>
|
/// 恢复区域文件夹里面的文件
|
/// </summary>
|
public static void RestoreRegionFiles (string regionRootPath)
|
{
|
DirectoryInfo TheFolder = new DirectoryInfo (regionRootPath);
|
int index = 0;
|
var thFolders = TheFolder.GetFiles ();
|
int folderCount = thFolders.Length;
|
foreach (FileInfo NextFile in thFolders) {
|
index++;
|
System.IO.FileInfo fileInfo = new System.IO.FileInfo (regionRootPath + NextFile.Name);
|
if (fileInfo.Exists) {
|
fileInfo.MoveTo (Application.RootPath + NextFile.Name);
|
Console.WriteLine ("Restroe : "+ NextFile.Name);
|
}
|
Application.RunOnMainThread (() => {
|
int pro = (int)(index * 1.0 / folderCount * 50) + 50;
|
Shared.SimpleControl.MainPage.Loading.Text = pro.ToString () + "%";
|
});
|
}
|
}
|
|
/// <summary>
|
/// 遍历文件
|
/// </summary>
|
public static List<string> ReadFiles ()
|
{
|
List<string> listFiles = new List<string> ();
|
DirectoryInfo TheFolder = new DirectoryInfo (RootPath);
|
foreach (FileInfo NextFile in TheFolder.GetFiles ()) {
|
listFiles.Add (NextFile.Name);
|
}
|
return listFiles;
|
}
|
|
/// <summary>
|
/// 更改文件名
|
/// </summary>
|
/// <param name="oldFilePath">Old file path.</param>
|
/// <param name="newFilePath">New file path.</param>
|
public static void ReNameFile (string oldFilePath, string newFilePath)
|
{
|
if (oldFilePath == newFilePath) {
|
return;
|
}
|
DeleteFile (newFilePath);
|
System.IO.FileInfo fileInfo = new System.IO.FileInfo (Application.RootPath + oldFilePath);
|
if (fileInfo.Exists) {
|
fileInfo.MoveTo (Application.RootPath + newFilePath);
|
}
|
//if (!UserConfig.Instance.LocalFiles.Contains (newFilePath)) {
|
// UserConfig.Instance.LocalFiles.Add (newFilePath);
|
//}
|
}
|
|
public static void ReNameFoler (string srcFolderPath, string destFolderPath)
|
{
|
if (srcFolderPath == destFolderPath) {
|
return;
|
}
|
var srcPath = Path.Combine (RootPath, srcFolderPath);
|
#if DEBUG
|
DirectoryInfo root = new DirectoryInfo (RootPath);
|
foreach (DirectoryInfo d in root.GetDirectories ()) {
|
Console.WriteLine ("文件夹:" + d.Name);
|
}
|
#endif
|
if (System.IO.Directory.Exists (srcPath)) {
|
System.IO.DirectoryInfo folder = new System.IO.DirectoryInfo (srcPath);
|
var path = Path.Combine (RootPath, destFolderPath);
|
if (Directory.Exists (path)) {
|
FileAttributes attr0 = File.GetAttributes (path);
|
if (attr0 == FileAttributes.Directory) {
|
Directory.Delete (path, true);
|
} else {
|
File.Delete (path);
|
}
|
}
|
folder.MoveTo (path);
|
|
|
#if DEBUG
|
List<string> listFiles = new List<string> ();
|
DirectoryInfo TheFolder = new DirectoryInfo (path);
|
foreach (FileInfo NextFile in TheFolder.GetFiles ()) {
|
Console.WriteLine (NextFile.Name);
|
}
|
#endif
|
}
|
|
#if DEBUG
|
foreach (DirectoryInfo d in root.GetDirectories ()) {
|
Console.WriteLine ("文件夹:" + d.Name);
|
}
|
#endif
|
}
|
|
/// <summary>
|
/// 文件是否存在
|
/// </summary>
|
/// <param name="filePath">File path.</param>
|
public static bool Exists (string filePath)
|
{
|
//System.Console.WriteLine ("Exists==" + System.IO.Path.Combine (RootPath, filePath));
|
return System.IO.File.Exists (System.IO.Path.Combine (RootPath, filePath));
|
}
|
|
/// <summary>
|
/// Deletes the file.
|
/// </summary>
|
/// <param name="fileName">File name.</param>
|
public static void DeleteFile (string fileName)
|
{
|
if (fileName == null || fileName == "Language.ini" || fileName == "AccountListDB") {
|
return;
|
}
|
System.IO.File.Delete (System.IO.Path.Combine (RootPath, fileName));
|
Console.WriteLine ("删除文件名为:" + fileName);
|
//if (UserConfig.Instance.LocalFiles.Contains (fileName)) {
|
// UserConfig.Instance.LocalFiles.Remove (fileName);
|
//}
|
}
|
|
/// <summary>
|
/// Deletes all file.
|
/// </summary>
|
public static void DeleteAllFile ()
|
{
|
List<string> filesList = ReadFiles ();
|
for (int j = 0; j < filesList.Count; j++) {
|
var f = filesList [j];
|
DeleteFile (f);
|
}
|
}
|
|
/// <summary>
|
/// Reads the equipment message.
|
/// </summary>
|
/// <returns>The equipment json data.</returns>
|
/// <param name="common">Common.</param>
|
/// <param name="isList">If set to <c>true</c> is list.</param>
|
///
|
/// <param name="commonType">
|
/// Commons是所有设备的基类,但是当传过来的Common是LightDevice时,LightDevice不是真正的设备,
|
/// 真正的设备是它下面的调光器(LightDimming)或者继电器.
|
/// 所以Common.GetType()获取不到真实的设备类型
|
/// 所以这里要手动传入设备的类型
|
/// </param>
|
public static string ReadEquipmentMessage (Common common)
|
{
|
string fileName = "Equipment_";
|
if (common.Type.ToString () == "WirelessPanel") {
|
fileName += DeviceType.ButtonPanel.ToString () + "_" + common.SubnetID.ToString () + "_" + common.DeviceID.ToString ();
|
} else {
|
fileName += common.GetType ().ToString ().Replace ("Shared.", "") + "_" + common.SubnetID.ToString () + "_" + common.DeviceID.ToString ();
|
}
|
byte [] bytes1 = ReadFile (fileName);
|
string one = System.Text.Encoding.UTF8.GetString (bytes1);
|
return one;
|
}
|
|
/// <summary>
|
/// Saves the equipment message.
|
/// </summary>
|
public static void SaveEquipmentMessage (Common common, string loopID = "")
|
{
|
string deviceType = common.Type.ToString ();
|
if (common.Type == DeviceType.LightSwitchSocket)
|
deviceType = DeviceType.LightSwitch.ToString ();
|
string fileName = "Equipment_" + deviceType + "_" + common.SubnetID.ToString () + "_" + common.DeviceID.ToString ();
|
if (loopID != "") {
|
fileName += "_" + (loopID.Length < 2 ? "0" + loopID : loopID);
|
}
|
common.SavePath = fileName;
|
Console.WriteLine (fileName);
|
WriteFileByBytes (fileName, SimpleControl.CommonPage.MyEncodingUTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (common)));
|
}
|
|
/// <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 static bool WriteFileByBytes (string fileName, byte [] bytes)
|
{
|
if (fileName == null || (fileName = fileName.Trim ()) == ("")) {
|
return false;
|
}
|
|
System.IO.FileStream fs = null;
|
|
try {
|
fs = new FileStream (System.IO.Path.Combine (RootPath, fileName), FileMode.Create, FileAccess.Write);
|
fs.Write (bytes, 0, bytes.Length);
|
fs.Flush ();
|
Console.WriteLine ("SaveFile:" + fileName);
|
//if (!UserConfig.Instance.LocalFiles.Contains (fileName)) {
|
// UserConfig.Instance.LocalFiles.Add (fileName);
|
//}
|
return true;
|
} catch (Exception ex) {
|
Console.WriteLine ("FileUtiles Code 113:" + ex.ToString ());
|
return false;
|
} finally {
|
try {
|
if (fs != null) {
|
fs.Close ();
|
}
|
} catch (Exception ex) {
|
Console.WriteLine ("FileUtils Code 121 :" + ex.ToString ());
|
}
|
}
|
}
|
|
/// <summary>
|
/// Writes the file by input stream.
|
/// </summary>
|
/// <returns><c>true</c>, if file by input stream was writed, <c>false</c> otherwise.</returns>
|
/// <param name="fileName">File name.</param>
|
/// <param name="inputStream">Input stream.</param>
|
/// <param name="isNeedRootPath">If set to <c>true</c> is need root path.</param>
|
public static bool WriteFileByInputStream (string fileName, System.IO.Stream inputStream)
|
{
|
if (fileName == null || (fileName = fileName.Trim ()) == ("")) {
|
return false;
|
}
|
System.IO.FileStream fs = null;
|
|
try {
|
byte [] buffer = new byte [4 * 1024];
|
int length = 0;
|
while ((length = inputStream.Read (buffer, 0, buffer.Length)) != 0) {
|
if (fs == null) {
|
fs = new FileStream (System.IO.Path.Combine (RootPath, fileName), FileMode.Create, FileAccess.Write);
|
}
|
fs.Write (buffer, 0, length);
|
fs.Flush ();
|
}
|
|
return true;
|
} catch {
|
return false;
|
} finally {
|
try {
|
if (fs != null) {
|
fs.Close ();
|
}
|
} catch {
|
}
|
}
|
}
|
|
/// <summary>
|
/// read file
|
/// </summary>
|
/// <returns>The file.</returns>
|
/// <param name="fileName">File name.</param>
|
public static byte [] ReadImage (string fileName)
|
{
|
if (Shared.Application.IsPad) {
|
fileName = "Pad/" + fileName;
|
} else {
|
fileName = "Phone/" + fileName;
|
}
|
System.IO.FileStream fs = null;
|
try {
|
if (System.IO.File.Exists (System.IO.Path.Combine (RootPath, fileName))) {
|
fs = new FileStream (System.IO.Path.Combine (RootPath, fileName), FileMode.Open, FileAccess.Read);
|
} else if (System.IO.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 static byte [] ReadFile (string fileName)
|
{
|
System.IO.FileStream fs = null;
|
try {
|
if (System.IO.File.Exists (System.IO.Path.Combine (RootPath, fileName))) {
|
fs = new FileStream (System.IO.Path.Combine (RootPath, fileName), FileMode.Open, FileAccess.Read);
|
} else if (System.IO.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>
|
/// 下载电台、音乐、DLNA等等网络图片
|
/// </summary>
|
/// <param name="fileName"></param>
|
/// <param name="url"></param>
|
public static void DownLoadImage (string fileName, string url)
|
{
|
#region 如果当前目录不存在,就初始化
|
string directioryPath = null;
|
if (Shared.Application.IsPad) {
|
directioryPath = "Pad/" + "MusicImage";
|
} else {
|
directioryPath = "Phone/" + "MusicImage";
|
}
|
directioryPath = System.IO.Path.Combine (RootPath, directioryPath);
|
if (!System.IO.Directory.Exists (directioryPath)) {
|
System.IO.Directory.CreateDirectory (directioryPath);
|
}
|
#endregion
|
|
if (!System.IO.File.Exists (fileName)) {
|
System.Threading.Tasks.Task.Run (() => {
|
FileStream fs = null;
|
try {
|
Shared.Net.MyWebClient webClient = new Shared.Net.MyWebClient ();
|
byte [] recevieBytes = webClient.DownloadData (new Uri (url));
|
fs = new FileStream (fileName, FileMode.Create, FileAccess.Write);
|
fs.Write (recevieBytes, 0, recevieBytes.Length);
|
fs.Flush ();
|
} catch { } finally {
|
if (fs != null) {
|
fs.Close ();
|
}
|
}
|
});
|
}
|
}
|
|
/// <summary>
|
/// 下载电台、音乐、DLNA等等网络图片
|
/// </summary>
|
/// <param name="fileName"></param>
|
/// <param name="url"></param>
|
public static string DownLoadImageFormBaidu (string album, string artist)
|
{
|
var url = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=qianqian&version=2.1.0&method=baidu.ting.search.catalogSug&format=json&query=" + artist;
|
string directioryPath = "MusicImage";
|
directioryPath = System.IO.Path.Combine (RootPath, directioryPath);
|
if (!System.IO.Directory.Exists (directioryPath)) {
|
System.IO.Directory.CreateDirectory (directioryPath);
|
}
|
try {
|
var song = Newtonsoft.Json.JsonConvert.DeserializeObject<Song> (new Net.MyWebClient ().DownloadString (new Uri (url)));
|
if(song==null){
|
return null;
|
}
|
foreach (var tempAlbum in song.album) {
|
if (tempAlbum.albumname == album && artist == tempAlbum.artistname) {
|
var bytes = new Shared.Net.MyWebClient ().DownloadData (new Uri (tempAlbum.artistpic.Split ('@') [0] + "@s_1,w_160,h_160"));
|
var filePath = System.IO.Path.Combine (directioryPath, "AlbumArtistImage_" + album);
|
var fs = new FileStream (filePath, FileMode.Create, FileAccess.Write);
|
fs.Write (bytes, 0, bytes.Length);
|
fs.Flush ();
|
fs.Close ();
|
return filePath;
|
}
|
}
|
|
foreach (var tempArtist in song.artist) {
|
if (artist == tempArtist.artistname) {
|
var bytes = new Shared.Net.MyWebClient ().DownloadData (new Uri (tempArtist.artistpic.Split ('@') [0] + "@s_1,w_160,h_160"));
|
var filePath = System.IO.Path.Combine (directioryPath, "AlbumArtistImage_" + artist);
|
var fs = new FileStream (filePath, FileMode.Create, FileAccess.Write);
|
fs.Write (bytes, 0, bytes.Length);
|
fs.Flush ();
|
fs.Close ();
|
return filePath;
|
}
|
}
|
|
} catch { }
|
return null;
|
}
|
|
}
|
[System.Serializable]
|
class Song
|
{
|
//string [] ss = new string [] { };
|
public List<Album> album = new List<Album> ();
|
public List<Artist> artist = new List<Artist> ();
|
public string sss;
|
}
|
[System.Serializable]
|
class Album
|
{
|
public string albumname;
|
public string artistname;
|
public string artistpic;
|
}
|
[System.Serializable]
|
class Artist
|
{
|
public string artistname;
|
public string artistpic;
|
}
|
|
|
}
|