#region License and Copyright /* ------------------------------------------------------------------------- * Dotnet Commons IO * * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the * * Free Software Foundation, Inc., * 59 Temple Place, * Suite 330, * Boston, * MA 02111-1307 * USA * * ------------------------------------------------------------------------- */ #endregion using System; using System.Collections; using System.Globalization; using System.IO; namespace Dotnet.Commons.IO { /// /// <summary> /// This class provides basic facilities for manipulating files and file paths. /// /// <h3>File-related methods</h3> /// There are methods to /// <list type="bullet"> /// <item>copy a file to another file,</item> /// <item>compare the content of 2 files,</item> /// <item>delete files using the wildcard character,</item> /// <item>etc</item> /// </list> /// </summary> /// public sealed class FileUtils { /// --------------------------------------------------------------- /// <summary> Tests if the specified <i>file</i> is newer than the reference /// <i>file</i>. /// /// </summary> /// <param name="newerFile">the <i>file</i> of which the modification date must be compared /// </param> /// <param name="referenceFile">the <i>file</i> of which the modification date is used /// like reference /// </param> /// <returns> true if the <i>file</i> exists and has been modified more recently /// than the reference <i>file</i>. /// </returns> /// --------------------------------------------------------------- public static bool IsNewer(FileInfo newerFile, FileInfo referenceFile) { if (referenceFile == null) throw new ArgumentNullException("referenceFile"); bool exists = referenceFile.Exists; if (!referenceFile.Exists) exists = Directory.Exists(referenceFile.FullName); if (!exists) throw new System.ArgumentException("The reference file '" + referenceFile + "' doesn't exist"); return IsNewer(newerFile, referenceFile.LastWriteTime); } /// --------------------------------------------------------------- /// <summary> /// Tests if the specified <i>file</i> is newer than the specified /// time reference. /// </summary> /// <param name="file">the <i>file</i> of which the modification date must be compared.</param> /// <param name="timeCompare">the time reference</param> /// <returns>true if the <i>file</i> exists and has been modified after /// the given time reference.</returns> /// --------------------------------------------------------------- public static bool IsNewer(FileInfo file, DateTime timeCompare) { if (file == null) throw new ArgumentException("No file specified"); bool exists = file.Exists; if (!file.Exists) exists = Directory.Exists(file.FullName); if (!exists) return false; return (file.LastWriteTime > timeCompare); } } }
Author: coder
Get all the files that matches a wildcard pattern, eg. (*.tmp)
#region License and Copyright /* ------------------------------------------------------------------------- * Dotnet Commons IO * * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the * * Free Software Foundation, Inc., * 59 Temple Place, * Suite 330, * Boston, * MA 02111-1307 * USA * * ------------------------------------------------------------------------- */ #endregion using System; using System.Collections; using System.Globalization; using System.IO; namespace Dotnet.Commons.IO { /// /// <summary> /// This class provides basic facilities for manipulating files and file paths. /// /// <h3>File-related methods</h3> /// There are methods to /// <list type="bullet"> /// <item>copy a file to another file,</item> /// <item>compare the content of 2 files,</item> /// <item>delete files using the wildcard character,</item> /// <item>etc</item> /// </list> /// </summary> /// public sealed class FileUtils { private static bool hasWildCards(string file) { return file.IndexOf("*") > -1; } /// --------------------------------------------------------------- /// <summary> /// Get all the files that matches a wildcard pattern, eg. (*.tmp) /// </summary> /// <param name="pathPattern">Wildcard pattern to search, eg. (Profile*.doc)</param> /// <returns>an array of FileInfo objects that results from the wildcard pattern file search</returns> /// --------------------------------------------------------------- public static FileInfo[] GetFilesMatchWildCard(string pathPattern) { FileInfo[] files = new FileInfo[0]; if (hasWildCards(pathPattern)) { string dir = Path.GetDirectoryName(pathPattern); DirectoryInfo info = new DirectoryInfo(dir); string pattern = Path.GetFileName(pathPattern); if (info.Exists) files = info.GetFiles(pattern); } else { files = new FileInfo[] {new FileInfo(pathPattern.Trim())}; } return files; } } }
Returns an array of abstract pathnames representing the files and directories of the specified path.
#region License and Copyright
/* ————————————————————————-
* Dotnet Commons IO
*
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the
*
* Free Software Foundation, Inc.,
* 59 Temple Place,
* Suite 330,
* Boston,
* MA 02111-1307
* USA
*
* ————————————————————————-
*/
#endregion
using System;
using System.Collections;
using System.Globalization;
using System.IO;
namespace Dotnet.Commons.IO
{
///
///
///
///
File-related methods
/// There are methods to
///
///
///
///
///
///
///
///
public sealed class FileUtils
{
/// —————————————————————
///
///
/// The abstract pathname to list it childs.
///
/// null if the path is not a directory
/// —————————————————————
public static FileInfo[] GetFiles(FileInfo path)
{
if ((path.Attributes & FileAttributes.Directory) > 0)
{
String[] fullpathnames = Directory.GetFileSystemEntries(path.FullName);
FileInfo[] result = new FileInfo[fullpathnames.Length];
for (int i = 0; i < result.Length; i++)
result[i] = new FileInfo(fullpathnames[i]);
return result;
}
return null;
}
}
}
[/csharp]
Read the content of the text file.
#region License and Copyright /* ------------------------------------------------------------------------- * Dotnet Commons IO * * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the * * Free Software Foundation, Inc., * 59 Temple Place, * Suite 330, * Boston, * MA 02111-1307 * USA * * ------------------------------------------------------------------------- */ #endregion using System; using System.Collections; using System.Globalization; using System.IO; namespace Dotnet.Commons.IO { /// /// <summary> /// This class provides basic facilities for manipulating files and file paths. /// /// <h3>File-related methods</h3> /// There are methods to /// <list type="bullet"> /// <item>copy a file to another file,</item> /// <item>compare the content of 2 files,</item> /// <item>delete files using the wildcard character,</item> /// <item>etc</item> /// </list> /// </summary> /// public sealed class FileUtils { /// --------------------------------------------------------------- /// <summary> /// Read the content of the text file. /// </summary> /// <param name="path">Full path (directory + filename) of file to read</param> /// <returns>content in string</returns> /// --------------------------------------------------------------- public static string ReadTextFile(string path) { string content = ""; FileStream stream = null; StreamReader reader = null; FileInfo fi = new FileInfo(path); if (!fi.Exists) throw new FileNotFoundException(string.Format("The file '{0}' does not exists", path)); try { stream = fi.OpenRead(); reader = new StreamReader(stream); content = reader.ReadToEnd(); } finally { if (stream != null) { stream.Close(); } if (reader != null) { reader.Close(); } } return content; } } }
Checks if the giving File exists, and returns its length
#region License and Copyright /* ------------------------------------------------------------------------- * Dotnet Commons IO * * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the * * Free Software Foundation, Inc., * 59 Temple Place, * Suite 330, * Boston, * MA 02111-1307 * USA * * ------------------------------------------------------------------------- */ #endregion using System; using System.Collections; using System.Globalization; using System.IO; namespace Dotnet.Commons.IO { /// /// <summary> /// This class provides basic facilities for manipulating files and file paths. /// /// <h3>File-related methods</h3> /// There are methods to /// <list type="bullet"> /// <item>copy a file to another file,</item> /// <item>compare the content of 2 files,</item> /// <item>delete files using the wildcard character,</item> /// <item>etc</item> /// </list> /// </summary> /// public sealed class FileUtils { /// --------------------------------------------------------------- /// <summary> /// Checks if the giving File exists, and returns its length /// </summary> /// <param name="file">The File instance to check</param> /// <returns>The length of the file if it exists, 0 otherwise</returns> /// --------------------------------------------------------------- public static long GetFileLength(FileInfo file) { if (file.Exists) return file.Length; else return 0; } } }
Gets information about the files in a directory and puts it in an array of strings.
#region License and Copyright
/* ————————————————————————-
* Dotnet Commons IO
*
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the
*
* Free Software Foundation, Inc.,
* 59 Temple Place,
* Suite 330,
* Boston,
* MA 02111-1307
* USA
*
* ————————————————————————-
*/
#endregion
using System;
using System.Collections;
using System.Globalization;
using System.IO;
namespace Dotnet.Commons.IO
{
///
///
///
///
File-related methods
/// There are methods to
///
///
///
///
///
///
///
///
public sealed class FileUtils
{
/// —————————————————————
///
/// The file attributes are separated by commas.
///
///
///
/// An string array containing comma separated values of
/// file information in a given directory
///
/// As the comma character is a valid character in a file name,
/// the values are encapsulated within a double
/// quote, eg. “Dotnet.Commons.IO.dll”,”28672″,”26/01/2006 2:25:26 AM”,”27/07/2006 10:18:04 PM”,”27/07/2006 10:16″,”Archive”
///
/// —————————————————————
public static string[] GetDirectoryFileInfo(string directory)
{
return GetDirectoryFileInfo(directory, ',');
}
private static string encapsulateInQuote(string value, bool toEncapsulate)
{
if (toEncapsulate)
return string.Format(“”{0}””, value);
else
return value;
}
/// —————————————————————
///
///
///
///
/// —————————————————————
public static FileInfo[] GetFilesInDirectory(string directory)
{
if ((directory == null) || (directory.Length < 1))
throw new System.ArgumentException("Directory supplied is either null or empty");
DirectoryInfo dirInfo = new DirectoryInfo(directory);
if (!dirInfo.Exists)
throw new System.ArgumentException("Directory '" + directory + "' does not exist.");
return dirInfo.GetFiles();
}
/// ---------------------------------------------------------------
///
///
/// name of directory to search
/// delimeter character to separator file attributes
///
/// An string array containing comma separated values of
/// file information in a given directory in the format:
///
/// filename,Size,CreationTime,LastAccessTime,LastWriteTime,Attributes ///
/// assuming that the delimeter chosen is the comma ',' character.
///
/// —————————————————————
public static string[] GetDirectoryFileInfo(string directory, char delimeter)
{
ArrayList al = new ArrayList();
al.Add(String.Format(“Name{0}Size{0}CreationTime{0}LastAccessTime{0}LastWriteTime{0}Attributes”, delimeter));
bool toEncapsulateInQuote = delimeter == ',';
FileInfo[] files = GetFilesInDirectory(directory);
for (int i = 0; i < files.Length; i++)
{
System.Text.StringBuilder buffy = new System.Text.StringBuilder();
buffy.Append(encapsulateInQuote(files[i].Name, toEncapsulateInQuote));
buffy.Append(delimeter);
buffy.Append(encapsulateInQuote(files[i].Length.ToString(), toEncapsulateInQuote));
buffy.Append(delimeter);
buffy.Append(encapsulateInQuote(files[i].CreationTime.ToString(), toEncapsulateInQuote));
buffy.Append(delimeter);
buffy.Append(encapsulateInQuote(files[i].LastAccessTime.ToString(), toEncapsulateInQuote));
buffy.Append(delimeter);
buffy.Append(encapsulateInQuote(files[i].LastWriteTime.ToString(), toEncapsulateInQuote));
buffy.Append(delimeter);
buffy.Append(encapsulateInQuote(files[i].Attributes.ToString(), toEncapsulateInQuote));
al.Add(buffy.ToString());
}
string[] dInfo = new string[al.Count];
al.CopyTo(dInfo);
return dInfo;
}
}
}
[/csharp]
Implements the same behaviour as the “touch” utility on Unix.
#region License and Copyright /* ------------------------------------------------------------------------- * Dotnet Commons IO * * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2.1 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the * * Free Software Foundation, Inc., * 59 Temple Place, * Suite 330, * Boston, * MA 02111-1307 * USA * * ------------------------------------------------------------------------- */ #endregion using System; using System.Collections; using System.Globalization; using System.IO; namespace Dotnet.Commons.IO { /// /// <summary> /// This class provides basic facilities for manipulating files and file paths. /// /// <h3>File-related methods</h3> /// There are methods to /// <list type="bullet"> /// <item>copy a file to another file,</item> /// <item>compare the content of 2 files,</item> /// <item>delete files using the wildcard character,</item> /// <item>etc</item> /// </list> /// </summary> /// public sealed class FileUtils { /// --------------------------------------------------------------- /// <summary> /// Implements the same behaviour as the "touch" utility on Unix. It creates /// a new file with size 0 or, if the file exists already, it is opened and /// closed without modifying it, but updating the file date and time. /// </summary> /// <param name="file"> File to touch</param> /// <exception cref="IOException">If an I/O problem occurs</exception> /// --------------------------------------------------------------- public static void Touch(FileInfo file) { if (file.Exists) { FileStream fs = file.OpenRead(); fs.Close(); file.LastAccessTime = DateTime.Now; file.Refresh(); } else { FileStream fs = file.OpenWrite(); fs.WriteByte(0); fs.Close(); file.Refresh(); } } } }