312 lines
8.6 KiB
C#
312 lines
8.6 KiB
C#
//-----------------------------------------------------------------------------
|
|
// <copyright file="File.cs" company="BTU/IIT">
|
|
// Company copyright tag.
|
|
// </copyright>
|
|
// <author>fiedlchr</author>
|
|
// <sience>03.05.2013</sience>
|
|
//-----------------------------------------------------------------------------
|
|
namespace CampusAppWP8.Utility
|
|
{
|
|
using System;
|
|
using System.IO;
|
|
using System.Threading;
|
|
using Windows.Storage;
|
|
|
|
/// <summary>
|
|
/// File class.
|
|
/// </summary>
|
|
public class File
|
|
{
|
|
/// <summary>
|
|
/// Folder object.
|
|
/// </summary>
|
|
private static readonly IStorageFolder LocalFolder = ApplicationData.Current.LocalFolder;
|
|
|
|
/// <summary>
|
|
/// File name.
|
|
/// </summary>
|
|
private string filename = string.Empty;
|
|
|
|
/// <summary>
|
|
/// Read type.
|
|
/// </summary>
|
|
private IOTypeRead readType;
|
|
|
|
/// <summary>
|
|
/// Write type.
|
|
/// </summary>
|
|
private IOTypeWrite writeType;
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="File" /> class.
|
|
/// </summary>
|
|
/// <param name="filename">file name</param>
|
|
/// <param name="read">read type</param>
|
|
/// <param name="write">write type</param>
|
|
public File(string filename, IOTypeRead read, IOTypeWrite write)
|
|
{
|
|
this.filename = filename;
|
|
this.readType = (read == IOTypeRead.INVALID) ? IOTypeRead.ReadAsync : read;
|
|
this.writeType = write;
|
|
}
|
|
|
|
/// <summary>
|
|
/// IO read type ENUM.
|
|
/// </summary>
|
|
public enum IOTypeRead
|
|
{
|
|
/// <summary>
|
|
/// Invalid/unset state.
|
|
/// </summary>
|
|
INVALID = 0,
|
|
|
|
/// <summary>
|
|
/// Sync read.
|
|
/// </summary>
|
|
ReadSync = 1,
|
|
|
|
/// <summary>
|
|
/// Async read.
|
|
/// </summary>
|
|
ReadAsync = 2
|
|
}
|
|
|
|
/// <summary>
|
|
/// IO write type ENUM.
|
|
/// </summary>
|
|
public enum IOTypeWrite
|
|
{
|
|
/// <summary>
|
|
/// Invalid/unset state.
|
|
/// </summary>
|
|
INVALID = 0,
|
|
|
|
/// <summary>
|
|
/// Sync write.
|
|
/// </summary>
|
|
WriteSync = 1,
|
|
|
|
/// <summary>
|
|
/// Async write.
|
|
/// </summary>
|
|
WriteAsync = 2,
|
|
|
|
/// <summary>
|
|
/// Read only, no writing.
|
|
/// </summary>
|
|
ReadOnly = 3
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delegation of the write callback function prototype.
|
|
/// </summary>
|
|
public delegate void WriteCallbackFunc();
|
|
|
|
/// <summary>
|
|
/// Read data from file to a string.
|
|
/// </summary>
|
|
/// <param name="ioType">read type</param>
|
|
/// <returns>data string</returns>
|
|
public string ReadFile(IOTypeRead ioType = IOTypeRead.INVALID)
|
|
{
|
|
string retValue = null;
|
|
|
|
if (this.Exist() == true)
|
|
{
|
|
IOTypeRead tempType = ioType;
|
|
|
|
if (tempType == IOTypeRead.INVALID)
|
|
{
|
|
tempType = this.readType;
|
|
}
|
|
|
|
if (tempType == IOTypeRead.ReadAsync)
|
|
{
|
|
// retValue = this.ReadAsync();
|
|
retValue = this.ReadSync();
|
|
}
|
|
else if (tempType == IOTypeRead.ReadSync)
|
|
{
|
|
retValue = this.ReadSync();
|
|
}
|
|
}
|
|
|
|
return retValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Write bytes to the file.
|
|
/// </summary>
|
|
/// <param name="data">data byte array</param>
|
|
/// <param name="ioType">write type</param>
|
|
public void WriteFile(byte[] data, WriteCallbackFunc onSavedCallback, WriteCallbackFunc onFailedCallback, IOTypeWrite ioType = IOTypeWrite.INVALID)
|
|
{
|
|
IOTypeWrite tempType = ioType;
|
|
|
|
if (tempType == IOTypeWrite.INVALID)
|
|
{
|
|
tempType = this.writeType;
|
|
}
|
|
|
|
/*
|
|
if (tempType == IOTypeWrite.WriteAsync)
|
|
{
|
|
this.WriteAsync(data, onSavedCallback, onFailedCallback);
|
|
}
|
|
else if (tempType == IOTypeWrite.WriteSync)
|
|
{
|
|
// this.WriteSync(data);
|
|
this.WriteAsync(data, onSavedCallback, onFailedCallback);
|
|
}
|
|
*/
|
|
|
|
Thread th = new Thread(delegate() { this.WriteAsync(data, onSavedCallback, onFailedCallback); });
|
|
th.Start();
|
|
//this.WriteAsync(data, onSavedCallback, onFailedCallback);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Return a info object of the file.
|
|
/// </summary>
|
|
/// <returns>info of the file</returns>
|
|
public FileInfo GetFileInfo()
|
|
{
|
|
FileInfo info = new FileInfo(File.LocalFolder.Path + "\\" + this.filename);
|
|
return info;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Check if a file is existing.
|
|
/// </summary>
|
|
/// <returns>true, if file exists, otherwise false</returns>
|
|
public bool Exist()
|
|
{
|
|
return this.GetFileInfo().Exists;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Read data synchronous from file.
|
|
/// </summary>
|
|
/// <returns>data string</returns>
|
|
private string ReadSync()
|
|
{
|
|
string retValue = null;
|
|
|
|
using (Stream fileStream = File.LocalFolder.OpenStreamForReadAsync(this.filename).Result)
|
|
{
|
|
using (StreamReader streamReader = new StreamReader(fileStream))
|
|
{
|
|
retValue = streamReader.ReadToEnd();
|
|
}
|
|
}
|
|
|
|
return retValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Read data asynchronous from file.
|
|
/// </summary>
|
|
/// <returns>data string</returns>
|
|
private string ReadAsync()
|
|
{
|
|
string retValue = string.Empty;
|
|
|
|
return retValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Write data synchronous to file.
|
|
/// </summary>
|
|
/// <param name="data">data array</param>
|
|
/// <returns>true, if succeeded</returns>
|
|
private bool WriteSync(byte[] data)
|
|
{
|
|
bool retValue = true;
|
|
|
|
return retValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Write data asynchronous to file.
|
|
/// </summary>
|
|
/// <param name="data">data array</param>
|
|
private async void WriteAsync(byte[] data, WriteCallbackFunc onSavedCallback, WriteCallbackFunc onFailedCallback)
|
|
{
|
|
Logger.LogMsg("writeasync file: " + this.filename);
|
|
StorageFile file = null;
|
|
Stream s = null;
|
|
int counter = 0;
|
|
|
|
// try opening the file
|
|
while ((file == null) && (counter < 10))
|
|
{
|
|
try
|
|
{
|
|
file = await File.LocalFolder.CreateFileAsync(this.filename, CreationCollisionOption.ReplaceExisting);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
counter++;
|
|
System.Threading.Thread.Sleep(10);
|
|
}
|
|
}
|
|
|
|
if (file == null)
|
|
{
|
|
if (onFailedCallback != null)
|
|
{
|
|
onFailedCallback();
|
|
}
|
|
return;
|
|
}
|
|
|
|
counter = 0;
|
|
// try to get a stream on the file
|
|
while ((s == null) && (counter < 10))
|
|
{
|
|
try
|
|
{
|
|
s = await file.OpenStreamForWriteAsync();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
counter++;
|
|
System.Threading.Thread.Sleep(10);
|
|
}
|
|
}
|
|
|
|
if (s == null)
|
|
{
|
|
if (onFailedCallback != null)
|
|
{
|
|
onFailedCallback();
|
|
}
|
|
return;
|
|
}
|
|
|
|
// writing data
|
|
try
|
|
{
|
|
await s.WriteAsync(data, 0, data.Length);
|
|
await s.FlushAsync();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
if (onFailedCallback != null)
|
|
{
|
|
onFailedCallback();
|
|
}
|
|
Logger.LogException(e);
|
|
s.Dispose();
|
|
return;
|
|
}
|
|
|
|
s.Dispose();
|
|
|
|
if (onSavedCallback != null)
|
|
{
|
|
onSavedCallback();
|
|
}
|
|
}
|
|
}
|
|
} |