illustrates the FileInfo class

image_pdfimage_print


   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

 /*
  Example15_5.cs illustrates the FileInfo class
*/

using System;
using System.Windows.Forms;
using System.IO;

public class Example15_5 
{
    
    [STAThread]
  public static void Main() 
  {

    // create and show an open file dialog
    OpenFileDialog dlgOpen = new OpenFileDialog();
    if (dlgOpen.ShowDialog() == DialogResult.OK)
    {
      // use the File class to return info about the file
      FileInfo fi = new FileInfo(dlgOpen.FileName);
      Console.WriteLine("Filename " + fi.FullName );
      Console.WriteLine(" Created at " + fi.CreationTime );
      Console.WriteLine(" Accessed at " + fi.LastAccessTime );
    }

  }

}


           
          


illustrates the FileAttributes enumeration

image_pdfimage_print


   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

 /*
  Example15_4.cs illustrates the FileAttributes enumeration
*/

using System;
using System.Windows.Forms;
using System.IO;

public class Example15_4 
{

  // the DecipherAttributes method turns file attributes
  // into something easier for people to read
  public static void DecipherAttributes(FileAttributes f) 
  {
    if ((f & FileAttributes.Archive) == FileAttributes.Archive)
      Console.WriteLine("Archive");
    if ((f & FileAttributes.Compressed) == FileAttributes.Compressed)
      Console.WriteLine("Compressed");
    if ((f & FileAttributes.Device) == FileAttributes.Device)
      Console.WriteLine("Device");
    if ((f & FileAttributes.Directory)   == FileAttributes.Directory)
      Console.WriteLine("Directory");
    if ((f & FileAttributes.Encrypted)  == FileAttributes.Encrypted)
      Console.WriteLine("Encrypted");
    if ((f & FileAttributes.Hidden)  == FileAttributes.Hidden)
      Console.WriteLine("Hidden");
    if ((f & FileAttributes.NotContentIndexed)  == FileAttributes.NotContentIndexed)
      Console.WriteLine("NotContentIndexed");
    if ((f & FileAttributes.Offline)  == FileAttributes.Offline)
      Console.WriteLine("Offline");
    if ((f & FileAttributes.ReadOnly)  == FileAttributes.ReadOnly)
      Console.WriteLine("ReadOnly");
    if ((f & FileAttributes.ReparsePoint)  == FileAttributes.ReparsePoint)
      Console.WriteLine("ReparsePoint");
    if ((f & FileAttributes.SparseFile)  == FileAttributes.SparseFile)
      Console.WriteLine("SparseFile");
    if ((f & FileAttributes.System)  == FileAttributes.System)
      Console.WriteLine("System");
    if ((f & FileAttributes.Temporary)  == FileAttributes.Temporary)
      Console.WriteLine("Temporary");
  }
    
    [STAThread]
  public static void Main() 
  {

    // create and show an open file dialog
    OpenFileDialog dlgOpen = new OpenFileDialog();
    if (dlgOpen.ShowDialog() == DialogResult.OK)
    {
      // retrieve and show the file attributes
      FileAttributes f = File.GetAttributes(dlgOpen.FileName);
      Console.WriteLine("Filename " + dlgOpen.FileName +
        " has attributes:");
      DecipherAttributes(f);
    }

  }

}


           
          


Get File Access Control

image_pdfimage_print
   




using System;
using System.IO;
using System.Security.AccessControl;

class MainClass {
    static void Main(string[] args) {
        FileStream stream;
        string fileName;

        fileName = Path.GetRandomFileName();
        using (stream = new FileStream(fileName, FileMode.Create)) {
            // Do something.
        }
        SetRule(fileName, "Everyone", FileSystemRights.Read, AccessControlType.Deny);

        try {
            stream = new FileStream(fileName, FileMode.Create);
        } catch (Exception ex) {
            Console.WriteLine(ex.ToString());
        } finally {
            stream.Close();
            stream.Dispose();
        }
    }

    static void AddRule(string filePath, string account, FileSystemRights rights, AccessControlType controlType) {
        FileSecurity fSecurity = File.GetAccessControl(filePath);
        fSecurity.AddAccessRule(new FileSystemAccessRule(account, rights, controlType));
        File.SetAccessControl(filePath, fSecurity);
    }
    static void SetRule(string filePath, string account, FileSystemRights rights, AccessControlType controlType) {
        FileSecurity fSecurity = File.GetAccessControl(filePath);
        fSecurity.ResetAccessRule(new FileSystemAccessRule(account, rights, controlType));
        File.SetAccessControl(filePath, fSecurity);
    }
}

          


Delete a file

image_pdfimage_print
   


using System;
using System.IO;

class MainClass {
    static void Main() {
        string tempFile = Path.GetTempFileName();

        Console.WriteLine("Using " + tempFile);

        using (FileStream fs = new FileStream(tempFile, FileMode.Open)) {
            // (Write some data.)
        }

        // Now delete the file.
        File.Delete(tempFile);

    }
}


          


Get file Creation Time

image_pdfimage_print


   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

 /*
  Example15_3.cs illustrates the File class
*/

using System;
using System.Windows.Forms;
using System.IO;

public class Example15_3 
{
    [STAThread]
  public static void Main() 
  {

    // create and show an open file dialog
    OpenFileDialog dlgOpen = new OpenFileDialog();
    if (dlgOpen.ShowDialog() == DialogResult.OK)
    {
      // use the File class to return info about the file
      string s = dlgOpen.FileName;
      Console.WriteLine("Filename " + s);
      Console.WriteLine(" Created at " + File.GetCreationTime(s));
      Console.WriteLine(" Accessed at " + 
       File.GetLastAccessTime(s));
    }

  }

}


           
          


Copy one folder to another folder

image_pdfimage_print
   
 

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

public class Main{
        public static void Copy(string SourcePath, string TargetPath){
            List<string> directories = new List<string>();

            string tmpDestination;

            directories.Add(SourcePath);

            while (directories.Count > 0)
            {
                string directory = directories[0];

                tmpDestination = directory.Replace(SourcePath, "");
                if (tmpDestination.Length >= 1 &amp;&amp; tmpDestination.Substring(0, 1) == "")
                {
                    tmpDestination = tmpDestination.Substring(1);
                }
                tmpDestination = Path.Combine(TargetPath, tmpDestination);

                Directory.CreateDirectory(tmpDestination);

                foreach (string file in Directory.GetFiles(directory))
                {
                    FileInfo theFile = new FileInfo(file);
                    File.Copy(file, Path.Combine(tmpDestination, theFile.Name));
                }

                foreach (string tmpdir in Directory.GetDirectories(directory))
                {
                    directories.Add(tmpdir);
                }
                directories.RemoveAt(0);
            }
        }
    }

   
     


Append a suffix (such as a date) to the name of the file.

image_pdfimage_print
   
 

// 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 Fields

    private static int unique;
    #endregion

    /// <summary>
    /// Append a suffix (such as a date) to the name of the file.
    /// </summary>
    /// <param name="filename"></param>
    /// <param name="suffix"></param>
    /// <returns></returns>
    public static string AppendSuffixToFilename(string filename, string suffix)
    {
      FileInfo fi = new FileInfo(filename);
      return string.Format(@"{0}{1}{2}{3}",
        fi.DirectoryName,
        Path.GetFileNameWithoutExtension(fi.FullName),
        suffix,
        fi.Extension
        );
    }
  }
}