using System; using System.IO; class Class1 { static void Main(string[] args) { string[] cla = Environment.GetCommandLineArgs(); if (cla.GetUpperBound(0) == 2) { FileInfo fi = new FileInfo(cla[1]); fi.MoveTo(cla[2]); Console.WriteLine("File Created : " + fi.CreationTime.ToString()); Console.WriteLine("Moved to : " + cla[2]); } else Console.WriteLine("Usage: mv <source file> <destination file>"); } }
Author: coder
Get File Attributes from FileInfo
using System; using System.IO; class Class1 { static void Main(string[] args) { string[] cla = Environment.GetCommandLineArgs(); if (cla.GetUpperBound(0) == 1) { FileInfo fi = new FileInfo(cla[1]); fi.Delete(); Console.WriteLine("File : " + cla[1]); Console.WriteLine("Attributes: " + fi.Attributes.ToString()); Console.WriteLine("File Deleted..."); } else Console.WriteLine("Usage: rm <filename>"); } }
Create Stream from FileInfo from OpenText method
using System; using System.IO; public class LicenseViewer { public static void Main() { FileInfo license = new FileInfo("c:LICENSE.txt"); StreamReader txtIn = license.OpenText(); String line; do { line = txtIn.ReadLine(); Console.WriteLine(line); } while (line != null); } }
Create FileStream from FileInfo
using System; using System.IO; class FileFileInfoApp { static void Main(string[] args) { FileInfo f = new FileInfo("Bar.txt"); FileStream fs = f.Create(); StreamWriter w = new StreamWriter(fs); w.Write("Hello World"); w.Close(); fs = f.Open(FileMode.Open, FileAccess.Read, FileShare.None); StreamReader r = new StreamReader(fs); string t; while ((t = r.ReadLine()) != null){ Console.WriteLine(t); } w.Close(); fs.Close(); f.Delete(); } }
Illustrates demanding permissions
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example19_5.cs illustrates demanding permissions */ using System; using System.IO; using System.Security.Permissions; [FileIOPermissionAttribute(SecurityAction.Demand, All=@"c: emp")] public class Example19_5 { public static void MakeFile() { // Create a new file to work with FileStream fsOut = File.Create(@"c: emp est.txt"); // Create a StreamWriter to handle writing StreamWriter sw = new StreamWriter(fsOut); // And write some data sw.WriteLine("'Twas brillig, and the slithy toves"); sw.WriteLine("Did gyre and gimble in the wabe."); sw.Flush(); sw.Close(); } }
Illustrates declarative role-based security
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example19_8.cs illustrates declarative role-based security */ using System; using System.IO; using System.Security.Permissions; [PrincipalPermissionAttribute(SecurityAction.Demand, Role="Administrators")] public class Example19_8 { public static void Main() { // Create a new file to work with FileStream fsOut = File.Create(@"c: emp est.txt"); // Create a StreamWriter to handle writing StreamWriter sw = new StreamWriter(fsOut); // And write some data sw.WriteLine("'Twas brillig, and the slithy toves"); sw.WriteLine("Did gyre and gimble in the wabe."); sw.Flush(); sw.Close(); } }
Illustrates encrypting a file
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example19_9.cs illustrates encrypting a file */ using System; using System.IO; using System.Security.Cryptography; public class Example19_9 { public static void Main() { // Create a new file to work with FileStream fsOut = File.Create(@"c: empencrypted.txt"); // Create a new crypto provider TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); // Create a cryptostream to encrypt to the filestream CryptoStream cs = new CryptoStream(fsOut, tdes.CreateEncryptor(), CryptoStreamMode.Write); // Create a StreamWriter to format the output StreamWriter sw = new StreamWriter(cs); // And write some data sw.WriteLine("'Twas brillig, and the slithy toves"); sw.WriteLine("Did gyre and gimble in the wabe."); sw.Flush(); sw.Close(); // save the key and IV for future use FileStream fsKeyOut = File.Create(@"c: empencrypted.key"); // use a BinaryWriter to write formatted data to the file BinaryWriter bw = new BinaryWriter(fsKeyOut); // write data to the file bw.Write( tdes.Key ); bw.Write( tdes.IV ); // flush and close bw.Flush(); bw.Close(); } }