|
using System;
using System.IO;
namespace VVX
{
/// <summary>
/// Simple class to simplify access to a few File methods, such as Delete
/// WARNING: By no means complete!!!
/// </summary>
class File
{
/// <summary>
/// Structure used to return information on the operation
/// </summary>
public struct FileResult
{
public bool Success;
public string Msg;
public FileResult(bool bDefault, string sMsg)
{
this.Success = bDefault;
this.Msg = "";
}
}
/// <summary>
/// STATIC: Tests for the existence of a file
/// </summary>
/// <param name="filename">name of the file</param>
/// <returns>'true' if found, 'false' otherwise</returns>
public static bool Exists(string filename)
{
bool bExists = false;
try
{
FileInfo fi = new FileInfo(filename);
bExists = fi.Exists;
}
catch (Exception ex)
{
FileResult frRet = new FileResult(true, "");
frRet.Success = false;
frRet.Msg = ex.ToString();
}
return bExists;
}
/// <summary>
/// STATIC: Tests if a file is readonly
/// </summary>
/// <param name="filename">name of the file</param>
/// <returns>'true' if readonly, 'false' otherwise</returns>
public static bool IsReadOnly(string filename)
{
bool bIsReadOnly = false;
try
{
FileInfo fi = new FileInfo(filename);
if (fi.Exists)
bIsReadOnly = fi.IsReadOnly;
}
catch (Exception ex)
{
FileResult frRet = new FileResult(true, "");
frRet.Success = false;
frRet.Msg = ex.ToString();
}
return bIsReadOnly;
}
/// <summary>
/// STATIC: Deletes a file
/// </summary>
/// <param name="filename">name of the file</param>
/// <returns>'true' if deleted, 'false' otherwise</returns>
public static bool Delete(string filename)
{
FileResult frRet = new FileResult(true, "");
return Delete(filename, ref frRet);
}
/// <summary>
/// STATIC: Deletes a file
/// </summary>
/// <param name="filename">name of the file</param>
/// <param name="frRet">instance of VVX.FileResult updated by delete request</param>
/// <returns>'true' if deleted, 'false' otherwise</returns>
public static bool Delete(string filename, ref FileResult frRet)
{
frRet = new FileResult(true,"");
try
{
FileInfo fi = new FileInfo(filename);
if (fi.Exists)
fi.Delete();
}
catch (Exception ex)
{
frRet.Success = false;
frRet.Msg = ex.ToString();
}
return frRet.Success;
}
/// <summary>
/// STATIC: Encrypts a file.
/// WARNING It can only be decrypted on the machine on which it was encrypted
/// </summary>
/// <param name="filename">name of the file</param>
/// <returns>'true' if encrypted, 'false' otherwise</returns>
public static bool Encrypt(string filename)
{
FileResult frRet = new FileResult(true, "");
return Encrypt(filename, ref frRet);
}
/// <summary>
/// STATIC: Encrypts a file.
/// WARNING It can only be decrypted on the machine on which it was encrypted
/// </summary>
/// <param name="filename">name of the file</param>
/// <param name="frRet">instance of VVX.FileResult updated by delete request</param>
/// <returns>'true' if encrypted, 'false' otherwise</returns>
public static bool Encrypt(string filename, ref FileResult frRet)
{
frRet = new FileResult(true, "");
try
{
FileInfo fi = new FileInfo(filename);
if (fi.Exists)
fi.Encrypt();
}
catch (Exception ex)
{
frRet.Success = false;
frRet.Msg = ex.ToString();
}
return frRet.Success;
}
/// <summary>
/// STATIC: Decrypts a file.
/// WARNING It can only be decrypted on the machine on which it was encrypted
/// </summary>
/// <param name="filename">name of the file</param>
/// <returns>'true' if decrypted, 'false' otherwise</returns>
public static bool Decrypt(string filename)
{
FileResult frRet = new FileResult(true, "");
return Decrypt(filename, ref frRet);
}
/// <summary>
/// Decrypts a file.
/// WARNING It can only be decrypted on the machine on which it was encrypted
/// </summary>
/// <param name="filename">name of the file</param>
/// <param name="frRet">instance of VVX.FileResult updated by delete request</param>
/// <returns>'true' if decrypted, 'false' otherwise</returns>
public static bool Decrypt(string filename, ref FileResult frRet)
{
frRet = new FileResult(true, "");
try
{
FileInfo fi = new FileInfo(filename);
if (fi.Exists)
fi.Decrypt();
}
catch (Exception ex)
{
frRet.Success = false;
frRet.Msg = ex.ToString();
}
return frRet.Success;
}
/// <summary>
/// STATIC: Extracts the name (and extension) from a fully qualiied
/// (i.e., including path) filename
/// </summary>
/// <param name="sPathAndName">name of file including drive, path, etc.</param>
/// <returns>name and extension (e.g., name.ext)</returns>
public static string DoExtractFilename(string sPathAndName)
{
try
{
FileInfo fi = new FileInfo(sPathAndName);
return fi.Name;
}
catch (Exception ex)
{
FileResult frRet = new FileResult(true, "");
frRet.Success = false;
frRet.Msg = ex.ToString();
}
return sPathAndName;
}
public static string DoGetLastWriteTime(string sPathAndName)
{
try
{
FileInfo fi = new FileInfo(sPathAndName);
return fi.LastWriteTime.ToString();
}
catch (Exception ex)
{
FileResult frRet = new FileResult(true, "");
frRet.Success = false;
frRet.Msg = ex.ToString();
}
return sPathAndName;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.