Files
win8phoneApp/CampusAppWP8/CampusAppWP8/utility/File.cs
Christian Fiedler 8335c47b0f #57 #90 #80
2013-07-12 18:08:17 +02:00

231 lines
6.3 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 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>
/// 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, IOTypeWrite ioType = IOTypeWrite.INVALID)
{
IOTypeWrite tempType = ioType;
if (tempType == IOTypeWrite.INVALID)
{
tempType = this.writeType;
}
if (tempType == IOTypeWrite.WriteAsync)
{
this.WriteAsync(data);
}
else if (tempType == IOTypeWrite.WriteSync)
{
// this.WriteSync(data);
this.WriteAsync(data);
}
}
/// <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)
{
var file = await File.LocalFolder.CreateFileAsync(this.filename, CreationCollisionOption.ReplaceExisting);
using (var s = await file.OpenStreamForWriteAsync())
{
await s.WriteAsync(data, 0, data.Length);
}
}
}
}