// Copyright (c) Microsoft Corporation. All rights reserved. // This file is best viewed using outline mode (Ctrl-M Ctrl-O) // // This program uses code hyperlinks available as part of the HyperAddin Visual Studio plug-in. // It is available from http://www.codeplex.com/hyperAddin // using System; using System.IO; namespace Utilities { static class StreamUtilities { public static void CopyFromFile(string fromFilePath, Stream toStream) { using (Stream fromStream = File.OpenRead(fromFilePath)) CopyStream(fromStream, toStream); } public static void CopyToFile(Stream fromStream, string toFilePath) { using (Stream toStream = File.Create(toFilePath)) CopyStream(fromStream, toStream); } /// <summary> /// CopyStream simply copies 'fromStream' to 'toStream' /// </summary> public static int CopyStream(Stream fromStream, Stream toStream) { byte[] buffer = new byte[8192]; int totalBytes = 0; for (; ; ) { int count = fromStream.Read(buffer, 0, buffer.Length); if (count == 0) break; toStream.Write(buffer, 0, count); totalBytes += count; } return totalBytes; } }; }
Author: coder
return true if the filename has the given attribute set
// crudwork // Copyright 2004 by Steve T. Pham (http://www.crudwork.com) // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 General Public License for more details. // // You should have received a copy of the GNU General Public License // along with This program. If not, see <http://www.gnu.org/licenses/>. using System; using System.Collections.Generic; using System.IO; using System.Text; using System.CodeDom.Compiler; using System.Text.RegularExpressions; namespace crudwork.Utilities { /// <summary> /// File Utility /// </summary> public static class FileUtil { #region HasAttribute /// <summary> /// return true if the filename has the given attribute set /// </summary> /// <param name="filename"></param> /// <param name="attr"></param> /// <returns></returns> public static bool HasAttribute(string filename, FileAttributes attr) { return (File.GetAttributes(filename) & attr) == attr; } #endregion } }
Get Files / Get Folders methods
// crudwork
// Copyright 2004 by Steve T. Pham (http://www.crudwork.com)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with This program. If not, see
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.CodeDom.Compiler;
using System.Text.RegularExpressions;
namespace crudwork.Utilities
{
///
///
public static class FileUtil
{
#region Enums
///
///
public enum FileOrderType
{
///
///
None = 0,
///
///
Filename = 1,
///
///
Extension = 2,
///
///
Size = 3,
///
///
LastWriteTime = 4,
///
///
CreationDate = 5,
}
#endregion
#region GetFiles / GetFolders methods
///
///
///
///
///
public static string[] MakeRelativePath(string[] fileList, string path)
{
List
for (int i = 0; i < fileList.Length; i++)
{
string file = fileList[i];
results.Add(file.Replace(path + "", ""));
}
return results.ToArray();
}
///
///
///
///
///
public static string[] OrderFileBy(string[] fileList, FileOrderType fileOrderType)
{
string[] orderKey = new string[fileList.Length];
string[] orderVal = new string[fileList.Length];
//int maskLength = StringUtil.MaxLength(fileList);
int maskLength = 100;
string maskFormat = String.Format(@”{{0,{0}}}”, maskLength);
for (int i = 0; i < fileList.Length; i++) { string filename = fileList[i]; string orderByKey; if (!File.Exists(filename)) throw new FileNotFoundException(filename); FileInfo fi = new FileInfo(filename); switch (fileOrderType) { case FileOrderType.None: orderByKey = ""; break; case FileOrderType.Filename: { orderByKey = String.Format(maskFormat, fi.Name); } break; case FileOrderType.LastWriteTime: { DateTime dt = fi.LastWriteTime; orderByKey = String.Format("{0:0000}{1:00}{2:00}{3:00}{4:00}{5:00}{6:000}", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Millisecond ); } break; default: throw new ArgumentOutOfRangeException("not supported: " + fileOrderType); } orderKey[i] = orderByKey; orderVal[i] = fileList[i]; } if (fileOrderType != FileOrderType.None) { Array.Sort(orderKey, orderVal); } return orderVal; } #endregion } } [/csharp]
Read the given filename and yield return a string
// crudwork // Copyright 2004 by Steve T. Pham (http://www.crudwork.com) // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 General Public License for more details. // // You should have received a copy of the GNU General Public License // along with This program. If not, see <http://www.gnu.org/licenses/>. using System; using System.Collections.Generic; using System.IO; using System.Text; using System.CodeDom.Compiler; using System.Text.RegularExpressions; namespace crudwork.Utilities { /// <summary> /// File Utility /// </summary> public static class FileUtil { #region ReadFile methods ///// <summary> ///// Read content of file and store into a string array. ///// </summary> ///// <param name="filename"></param> ///// <returns></returns> //public static string[] ReadFile(string filename) //{ // try // { // List<String> results = new List<string>(); // using (StreamReader r = new StreamReader(filename)) // { // while (!r.EndOfStream) // { // results.Add(r.ReadLine()); // } // r.Close(); // } // return results.ToArray(); // } // catch (Exception ex) // { // DebuggerTool.AddData(ex, "filename", filename); // throw; // } //} /// <summary> /// Read the given filename and yield return a string /// </summary> /// <param name="filename"></param> /// <returns></returns> public static IEnumerable<string> ReadFile(string filename) { using (StreamReader r = new StreamReader(filename)) { while (!r.EndOfStream) { string line = r.ReadLine(); yield return line; } r.Close(); } yield break; } ///// <summary> ///// Read the given filename and return a byte array ///// </summary> ///// <param name="filename"></param> ///// <param name="bufSize"></param> ///// <returns></returns> //public static byte[] ReadFile(string filename, int bufSize) //{ // StringBuilder s = new StringBuilder(); // List<byte> results = null; // using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, bufSize)) // using (BinaryReader r = new BinaryReader(fs)) // { // results = new List<byte>((int)fs.Length); // byte[] readChar = null; // do // { // readChar = r.ReadBytes(bufSize); // results.AddRange(readChar); // } while ((readChar != null) && (readChar.Length > 0)); // r.Close(); // fs.Close(); // } // return results.ToArray(); //} /// <summary> /// Read the filename and yield return a byte array /// </summary> /// <param name="filename"></param> /// <param name="bufSize"></param> /// <returns></returns> public static IEnumerable<byte[]> ReadFile(string filename, int bufSize) { return ReadFile(filename, bufSize, 0); } /// <summary> /// Read the filename, start a the specified position, and yield return a byte array /// </summary> /// <param name="filename">type input file</param> /// <param name="bufSize">this bufSize will be multiple by 10</param> /// <param name="startAtPosition">set the starting position</param> /// <returns></returns> public static IEnumerable<byte[]> ReadFile(string filename, int bufSize, int startAtPosition) { using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read, bufSize * 10)) using (BinaryReader r = new BinaryReader(fs)) { fs.Position = startAtPosition; byte[] readChar = null; do { readChar = r.ReadBytes(bufSize); if (readChar != null) yield return readChar; } while ((readChar != null) && (readChar.Length > 0)); r.Close(); fs.Close(); } yield break; } #endregion } }
Copy a file to a different filename, with cleaning null characters.
// crudwork // Copyright 2004 by Steve T. Pham (http://www.crudwork.com) // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 General Public License for more details. // // You should have received a copy of the GNU General Public License // along with This program. If not, see <http://www.gnu.org/licenses/>. using System; using System.Collections.Generic; using System.IO; using System.Text; using System.CodeDom.Compiler; using System.Text.RegularExpressions; namespace crudwork.Utilities { /// <summary> /// File Utility /// </summary> public static class FileUtil { #region CleanNull /// <summary> /// Copy a file to a different filename, with cleaning null characters. /// </summary> /// <param name="filename"></param> /// <param name="outfile"></param> public static void CleanNull(string filename, string outfile) { try { if (File.Exists(outfile)) File.Delete(outfile); using (StreamReader sr = new StreamReader(filename)) using (BinaryReader br = new BinaryReader(sr.BaseStream)) using (StreamWriter sw = new StreamWriter(outfile)) using (BinaryWriter bw = new BinaryWriter(sw.BaseStream)) { while (br.PeekChar() != -1) { byte b = br.ReadByte(); // skip NULL character if (b == 0) continue; bw.Write(b); } sw.Flush(); sw.Close(); sr.Close(); } } catch (Exception ex) { throw; } } #endregion } }
Append File methods
// crudwork // Copyright 2004 by Steve T. Pham (http://www.crudwork.com) // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 General Public License for more details. // // You should have received a copy of the GNU General Public License // along with This program. If not, see <http://www.gnu.org/licenses/>. using System; using System.Collections.Generic; using System.IO; using System.Text; using System.CodeDom.Compiler; using System.Text.RegularExpressions; namespace crudwork.Utilities { /// <summary> /// File Utility /// </summary> public static class FileUtil { #region AppendFile methods /// <summary> /// Append a string value to a filename /// </summary> /// <param name="filename"></param> /// <param name="entry"></param> public static void AppendFile(string filename, string entry) { using (StreamWriter w = new StreamWriter(filename, true)) { w.WriteLine(entry); w.Flush(); w.Close(); } } #endregion } }
Write File methods
// crudwork // Copyright 2004 by Steve T. Pham (http://www.crudwork.com) // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program 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 General Public License for more details. // // You should have received a copy of the GNU General Public License // along with This program. If not, see <http://www.gnu.org/licenses/>. using System; using System.Collections.Generic; using System.IO; using System.Text; using System.CodeDom.Compiler; using System.Text.RegularExpressions; namespace crudwork.Utilities { /// <summary> /// File Utility /// </summary> public static class FileUtil { #region WriteFile methods /// <summary> /// Write a byte array to filename /// </summary> /// <param name="filename"></param> /// <param name="content"></param> public static void WriteFile(string filename, byte[] content) { int bufSize = 1024; try { using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Read, bufSize)) using (BinaryWriter w = new BinaryWriter(fs)) { for (int i = 0; i < content.Length; i++) { w.Write(content[i]); } w.Flush(); w.Close(); } } catch (Exception ex) { throw; } } /// <summary> /// Write a string array to a file. /// </summary> /// <param name="filename"></param> /// <param name="content"></param> public static void WriteFile(string filename, string[] content) { try { using (StreamWriter w = new StreamWriter(filename, false)) { for (int i = 0; i < content.Length; i++) { w.WriteLine(content[i]); } w.Flush(); w.Close(); } } catch (Exception ex) { throw; } } /// <summary> /// Write a string to a file. /// </summary> /// <param name="filename"></param> /// <param name="content"></param> public static void WriteFile(string filename, string content) { WriteFile(filename, new string[] { content }); } #endregion } }