/*
Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
#region Usings
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
#endregion
namespace Utilities
{
public static class FileManager
{
/// <summary>
/// Saves a file to an FTP server
/// </summary>
/// <param name="Content">File content</param>
/// <param name="FileName">File name to save this as (should include directories if applicable)</param>
/// <param name="FTPServer">Location of the ftp server</param>
/// <param name="UserName">User name to log in</param>
/// <param name="Password">Password to log in</param>
public static void SaveFile(string Content, string FileName, Uri FTPServer, string UserName, string Password)
{
try
{
Uri TempURI = new Uri(Path.Combine(FTPServer.ToString(), FileName));
FtpWebRequest FTPRequest = (FtpWebRequest)FtpWebRequest.Create(TempURI);
FTPRequest.Credentials = new NetworkCredential(UserName, Password);
FTPRequest.KeepAlive = false;
FTPRequest.Method = WebRequestMethods.Ftp.UploadFile;
FTPRequest.UseBinary = true;
FTPRequest.ContentLength = Content.Length;
FTPRequest.Proxy = null;
using (Stream TempStream = FTPRequest.GetRequestStream())
{
System.Text.ASCIIEncoding TempEncoding = new System.Text.ASCIIEncoding();
byte[] TempBytes = TempEncoding.GetBytes(Content);
TempStream.Write(TempBytes, 0, TempBytes.Length);
}
FTPRequest.GetResponse();
}
catch { }
}
}
}
Month: February 2011
Compares 2 files and determines if they are the same or not
/*
Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
#region Usings
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
#endregion
namespace Utilities
{
public static class FileManager
{
/// <summary>
/// Gets a files' contents
/// </summary>
/// <param name="FileName">File name</param>
/// <returns>a string containing the file's contents</returns>
public static string GetFileContents(string FileName)
{
try
{
return GetFileContents(FileName, 5000);
}
catch { throw; }
}
/// <summary>
/// Gets a files' contents
/// </summary>
/// <param name="FileName">File name</param>
/// <param name="TimeOut">Amount of time in ms to wait for the file</param>
/// <returns>a string containing the file's contents</returns>
public static string GetFileContents(string FileName, int TimeOut)
{
StreamReader Reader = null;
int StartTime = System.Environment.TickCount;
try
{
bool Opened = false;
while (!Opened)
{
try
{
if (System.Environment.TickCount - StartTime >= TimeOut)
throw new System.IO.IOException("File opening timed out");
Reader = File.OpenText(FileName);
Opened = true;
}
catch (System.IO.IOException e)
{
throw e;
}
}
string Contents = Reader.ReadToEnd();
Reader.Close();
return Contents;
}
catch
{
return "";
}
finally
{
if (Reader != null)
{
Reader.Close();
Reader.Dispose();
}
}
}
/// <summary>
/// Compares 2 files and determines if they are the same or not
/// </summary>
/// <param name="FileName1">name of the first file</param>
/// <param name="FileName2">name of the second file</param>
/// <returns>False if they are different, otherwise it returns true.</returns>
public static bool CompareFiles(string FileName1, string FileName2)
{
try
{
FileInfo File1 = new FileInfo(FileName1);
FileInfo File2 = new FileInfo(FileName2);
if (File1.Length != File2.Length)
{
return false;
}
string File1Contents = FileManager.GetFileContents(FileName1);
string File2Contents = FileManager.GetFileContents(FileName2);
if (!File1Contents.Equals(File2Contents))
return false;
return true;
}
catch (Exception a)
{
throw a;
}
}
}
}
Deletes files newer than the specified date
/*
Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
#region Usings
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
#endregion
namespace Utilities
{
public static class FileManager
{
/// <summary>
/// Determines if a directory exists
/// </summary>
/// <param name="DirectoryPath">Path of the directory</param>
/// <returns>true if it exists, false otherwise</returns>
public static bool DirectoryExists(string DirectoryPath)
{
try
{
return Directory.Exists(DirectoryPath);
}
catch (Exception a)
{
throw a;
}
}
/// <summary>
/// Gets a list of files
/// </summary>
/// <param name="DirectoryPath">Directory to check for files</param>
/// <param name="Recursive">Determines if this is a recursive look at all directories under this one</param>
/// <returns>a list of files</returns>
public static List<FileInfo> FileList(string DirectoryPath, bool Recursive)
{
try
{
if (!Recursive)
{
return FileList(DirectoryPath);
}
List<FileInfo> Files = new List<FileInfo>();
if (DirectoryExists(DirectoryPath))
{
DirectoryInfo Directory = new DirectoryInfo(DirectoryPath);
FileInfo[] SubFiles = Directory.GetFiles();
foreach (FileInfo SubFile in SubFiles)
{
Files.Add(SubFile);
}
DirectoryInfo[] SubDirectories = Directory.GetDirectories();
foreach (DirectoryInfo SubDirectory in SubDirectories)
{
List<FileInfo> TempFiles = FileList(SubDirectory.FullName, Recursive);
foreach (FileInfo File in TempFiles)
{
Files.Add(File);
}
}
}
return Files;
}
catch (Exception a)
{
throw a;
}
}
/// <summary>
/// Gets a list of files
/// </summary>
/// <param name="DirectoryPath">Directory to check for files</param>
/// <returns>a list of files</returns>
public static List<FileInfo> FileList(string DirectoryPath)
{
try
{
List<FileInfo> Files = new List<FileInfo>();
if (DirectoryExists(DirectoryPath))
{
DirectoryInfo Directory = new DirectoryInfo(DirectoryPath);
FileInfo[] SubFiles = Directory.GetFiles();
foreach (FileInfo SubFile in SubFiles)
{
Files.Add(SubFile);
}
}
return Files;
}
catch (Exception a)
{
throw a;
}
}
/// <summary>
/// Deletes files newer than the specified date
/// </summary>
/// <param name="Directory">Directory to look within</param>
/// <param name="CompareDate">The date to compare to</param>
/// <param name="Recursive">Is this a recursive call</param>
public static void DeleteFilesNewerThan(string Directory, DateTime CompareDate, bool Recursive)
{
try
{
List<FileInfo> Files = FileManager.FileList(Directory, Recursive);
foreach (FileInfo File in Files)
{
if (File.LastWriteTime > CompareDate)
{
File.Delete();
}
}
}
catch (Exception a)
{
throw a;
}
}
}
}
Determines if a directory exists
/*
Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
#region Usings
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
#endregion
namespace Utilities
{
/// <summary>
/// Utility class for managing files
/// </summary>
public static class FileManager
{
/// <summary>
/// Determines if a directory exists
/// </summary>
/// <param name="DirectoryPath">Path of the directory</param>
/// <returns>true if it exists, false otherwise</returns>
public static bool DirectoryExists(string DirectoryPath)
{
try
{
return Directory.Exists(DirectoryPath);
}
catch (Exception a)
{
throw a;
}
}
}
}
Renames a file
/*
Copyright (c) 2010 <a href="http://www.gutgames.com">James Craig</a>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
#region Usings
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
#endregion
namespace Utilities
{
/// <summary>
/// Utility class for managing files
/// </summary>
public static class FileManager
{
/// <summary>
/// Renames a file
/// </summary>
/// <param name="FileName">Original file</param>
/// <param name="NewFileName">New file name</param>
public static void RenameFile(string FileName, string NewFileName)
{
try
{
File.Move(FileName, NewFileName);
}
catch (Exception a)
{
throw a;
}
}
}
}
Saves a file
/*
Copyright (c) 2010 James Craig
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/
#region Usings
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
#endregion
namespace Utilities
{
///
///
public static class FileManager
{
///
///
/// Path of the directory
///
public static bool DirectoryExists(string DirectoryPath)
{
try
{
return Directory.Exists(DirectoryPath);
}
catch (Exception a)
{
throw a;
}
}
///
///
/// Content of the file
/// Path of the file
/// Tells the system if you wish to append data or create a new document
public static void SaveFile(string Content, string FileName,bool Append)
{
FileStream Writer = null;
try
{
byte[] ContentBytes = Encoding.UTF8.GetBytes(Content);
int Index = FileName.LastIndexOf('/');
if (Index <= 0)
{
Index = FileName.LastIndexOf('');
}
if (Index <= 0)
{
throw new Exception("Directory must be specified for the file");
}
string Directory = FileName.Remove(Index) + "/";
bool Opened = false;
while (!Opened)
{
try
{
if (Append)
{
Writer = File.Open(FileName, FileMode.Append, FileAccess.Write, FileShare.None);
}
else
{
Writer = File.Open(FileName, FileMode.Create, FileAccess.Write, FileShare.None);
}
Opened = true;
}
catch (System.IO.IOException e)
{
throw e;
}
}
Writer.Write(ContentBytes, 0, ContentBytes.Length);
Writer.Close();
}
catch (Exception a)
{
throw a;
}
finally
{
if (Writer != null)
{
Writer.Close();
Writer.Dispose();
}
}
}
}
}
[/csharp]
Get File SystemInfo
#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion
using System;
using System.Text;
using System.IO;
using System.Web;
using System.Web.Hosting;
namespace Utilities.IO
{
public class FileUtils
{
public static FileSystemInfo GetFileSystemInfo(string path)
{
if (File.Exists(path))
{
return new FileInfo(path);
}
else if (Directory.Exists(path))
{
return new DirectoryInfo(path);
}
return null;
}
}
}

