using System; using System.IO; public class WildcardTest { private static void Main(string[] args) { DirectoryInfo dir = new DirectoryInfo("c:"); FileInfo[] files = dir.GetFiles("*"); foreach (FileInfo file in files) { Console.Write("Name: " + file.Name + " "); Console.WriteLine("Size: " + file.Length.ToString()); } } }
Builds a CSV list from the specified int[], separator String and quote String
//
// (C) Copyright 2009 Irantha Suwandarathna (irantha@gmail.com)
// All rights reserved.
//
/* Copyright (c) 2001-2008, The HSQL Development Group
* All rights reserved.
*
* Redistribution and use _in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions _in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer _in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the HSQL Development Group nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Text;
using System.Collections.Generic;
namespace EffiProz.Core.Lib
{
/** Provides a collection of convenience methods for processing and
* creating objects with String
value components.
*
* @author fredt@users
* @author boucherb@users
* @version 1.7.2
* @since 1.7.0
*/
public class StringUtil
{
//public static string getList(Object[] s, string separator, string quote)
//{
// int len = s.Length;
// StringBuilder b = new StringBuilder(len * 16);
// for (int i = 0; i < len; i++)
// {
// b.Append(quote);
// b.Append(s[i]);
// b.Append(quote);
// if (i + 1 < len)
// {
// b.Append(separator);
// }
// }
// return b.ToString();
//}
/**
* Builds a CSV list from the specified int[], separator
* String
and quote
String
.
*
*
-
*
- All arguments are assumed to be non-null.
* - Separates each list element with the value of the
*separator
argument.
* - Prepends and appends each element with the value of the
*quote
argument.
*-
* @return a CSV list
* @param s the array of int values
* @param separator theString
to use as the separator
* @param quote theString
with which to quote the list elements
*/
public static string getList(int[] s, string separator, string quote)
{int len = s.Length;
StringBuilder b = new StringBuilder(len * 8);for (int i = 0; i < len; i++) { b.Append(quote); b.Append(s[i]); b.Append(quote); if (i + 1 < len) { b.Append(separator); } } return b.ToString(); } } } [/csharp]
Builds a CSV list from the specified String[], separator string and quote string.
//
// (C) Copyright 2009 Irantha Suwandarathna (irantha@gmail.com)
// All rights reserved.
//
/* Copyright (c) 2001-2008, The HSQL Development Group
* All rights reserved.
*
* Redistribution and use _in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions _in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer _in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the HSQL Development Group nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Text;
using System.Collections.Generic;
namespace EffiProz.Core.Lib
{
/** Provides a collection of convenience methods for processing and
* creating objects with String
value components.
*
* @author fredt@users
* @author boucherb@users
* @version 1.7.2
* @since 1.7.0
*/
public class StringUtil
{
///**
// * Builds a bracketed CSV list from the array
// * @param array an array of Objects
// * @return string
// */
//public static string arrayToString(Object array)
//{
// int len = ((Array)array).Length;
// int last = len – 1;
// StringBuilder sb = new StringBuilder(2 * (len + 1));
// sb.Append('{');
// for (int i = 0; i < len; i++) // { // sb.Append(((char[])array)[i]); // if (i != last) // { // sb.Append(','); // } // } // sb.Append('}'); // return sb.ToString(); //} /** * Builds a CSV list from the specified String[], separator string and * quote string.
*
*
-
*
- All arguments are assumed to be non-null.
* - Separates each list element with the value of the
*separator
argument.
* - Prepends and appends each element with the value of the
*quote
argument.
* - No attempt is made to escape the quote character sequence if it is
* found public to a list element.
*-
* @return a CSV list
* @param separator theString
to use as the list element separator
* @param quote theString
with which to quote the list elements
* @param s array ofString
objects
*/
public static string getList(String[] s, string separator, string quote)
{int len = s.Length;
StringBuilder b = new StringBuilder(len * 16);for (int i = 0; i < len; i++) { b.Append(quote); b.Append(s[i]); b.Append(quote); if (i + 1 < len) { b.Append(separator); } } return b.ToString(); } } } [/csharp]
Builds a bracketed CSV list from the array
//
// (C) Copyright 2009 Irantha Suwandarathna (irantha@gmail.com)
// All rights reserved.
//
/* Copyright (c) 2001-2008, The HSQL Development Group
* All rights reserved.
*
* Redistribution and use _in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions _in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer _in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the HSQL Development Group nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Text;
using System.Collections.Generic;
namespace EffiProz.Core.Lib
{
/** Provides a collection of convenience methods for processing and
* creating objects with String
value components.
*
* @author fredt@users
* @author boucherb@users
* @version 1.7.2
* @since 1.7.0
*/
public class StringUtil
{
/**
* Builds a bracketed CSV list from the array
* @param array an array of Objects
* @return string
*/
public static String arrayToString(Object array)
{
int len = ((Array)array).Length;
int last = len – 1;
StringBuilder sb = new StringBuilder(2 * (len + 1));
sb.Append('{');
for (int i = 0; i < len; i++) { sb.Append(((Array)array).GetValue(i)); if (i != last) { sb.Append(','); } } sb.Append('}'); return sb.ToString(); } } } [/csharp]
Save a byte array content into a 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> /// Save a byte array content into a file. /// </summary> /// <param name="path">The full path of the file to save</param> /// <param name="content">content to save in byte array</param> /// --------------------------------------------------------------- public static void Save(string path, byte[] content) { if (Path.GetDirectoryName(path).Length > 0 && !Directory.Exists(Path.GetDirectoryName(path))) throw new DirectoryNotFoundException(String.Format("The directory path '{0}' to save the file does not exist.", Path.GetDirectoryName(path))); using (FileStream writeStream = new FileStream(path, FileMode.Create, FileAccess.Write)) { writeStream.Write(content, 0, content.Length); writeStream.Close(); } } /// --------------------------------------------------------------- /// <summary> /// Save a char array content into a file. /// </summary> /// <param name="path">The full path of the file to save.</param> /// <param name="content">content to save in char array.</param> /// --------------------------------------------------------------- public static void Save(string path, char[] content) { // convert char[] to byte[] System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); byte[] byteArray = encoding.GetBytes(content); Save(path, byteArray); } /// --------------------------------------------------------------- /// <summary> /// Save a string content into a file. /// </summary> /// <param name="path">The full path of the file to save</param> /// <param name="content">content to save in string</param> /// --------------------------------------------------------------- public static void Save(string path, string content) { if (Path.GetDirectoryName(path).Length > 0 && !Directory.Exists(Path.GetDirectoryName(path))) throw new DirectoryNotFoundException(String.Format("The directory path '{0}' to save the file does not exist.", Path.GetDirectoryName(path))); using (StreamWriter stream = File.CreateText(path)) { stream.Write(content); stream.Close(); } } } }
Read binary context of a 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> /// Reads data from the beginning of a stream until the end is reached. The /// data is returned as a byte array. /// </summary> /// <param name="stream">The stream to read data from</param> /// <exception cref="IOException">thrown if any of the underlying IO calls fail</exception> /// <remarks>Use this method if you don't know the length of the stream in advance /// (for instance a network stream) and just want to read the whole lot into a buffer. /// <para> /// <strong>Note:</strong><br/> /// This method of reading the stream is not terribly efficient. /// </para> /// </remarks> public static byte[] GetBytes(Stream stream) { if (stream is MemoryStream) return ((MemoryStream)stream).ToArray(); byte[] byteArray = new byte[1024]; using (MemoryStream ms = new MemoryStream()) { stream.Position = 0; while (true) { int readLen = stream.Read(byteArray, 0, byteArray.Length); if (readLen <= 0) return ms.ToArray(); ms.Write(byteArray, 0, readLen); } } } /// <summary> /// Reads data from a stream until the end is reached. The /// data is returned as a byte array. /// </summary> /// <param name="stream">The stream to read data from</param> /// <param name="initialLength">The initial buffer length. If the length is &lt; 1, /// then the default value of <see cref="Int16.MaxValue"/> will be used. /// </param> /// <exception cref="IOException">thrown if any of the underlying IO calls fail</exception> /// <remarks>Use this method to get the data if you know the expected length of data to start with.</remarks> public static byte[] GetBytes(Stream stream, long initialLength) { // If we've been passed an unhelpful initial length, just // use 32K. if (initialLength < 1) initialLength = Int16.MaxValue; byte[] buffer = new byte[initialLength]; int read = 0; int chunk; while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0) { read += chunk; // If we've reached the end of our buffer, check to see if there's // any more information if (read == buffer.Length) { int nextByte = stream.ReadByte(); // End of stream? If so, we're done if (nextByte == -1) { return buffer; } // Nope. Resize the buffer, put in the byte we've just // read, and continue byte[] newBuffer = new byte[buffer.Length * 2]; Array.Copy(buffer, newBuffer, buffer.Length); newBuffer[read] = (byte)nextByte; buffer = newBuffer; read++; } } // Buffer is now too big. Shrink it. byte[] ret = new byte[read]; Array.Copy(buffer, ret, read); return ret; } /// --------------------------------------------------------------- /// <summary> /// Read binary context of a file. /// </summary> /// <param name="path">Full path (directory + filename) of file to read</param> /// <returns>content in byte array</returns> /// --------------------------------------------------------------- public static byte[] ReadBinaryFile(string path) { FileInfo fi = new FileInfo(path); byte[] data = null; if (!fi.Exists) throw new IOException(path + " does not exists"); // Read File using (Stream stream = File.OpenRead(path)) { // Get Data from The Stream data = GetBytes(stream, fi.Length); // Close Stream stream.Close(); } fi.Refresh(); // Return Data return (data); } } }
Read file content to a byte array
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; public class File { public static byte[] Read(string fileName) { byte[] buff = null; FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); long numBytes = new FileInfo(fileName).Length; buff = br.ReadBytes((int)numBytes); return buff; } }