15812_file security1

32
File creation , writing and reading cl ass Program { static void Main( string[] ar gs) { using (StreamWriter sw = new StreamWriter("F:\\TestFile1.txt")) // file creation { // Add some text to the file. sw.Write("This is the "); sw.WriteLine(" header f or the file. is a good boy"); sw.WriteLine("-------------------"); // Arbitr ary ob jec ts can also be written to the file. sw.Writ e(" The date is: "); sw.WriteLine(DateTime.Now); }

Upload: tejasvi-mahajan

Post on 06-Apr-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 1/32

File creation , writing and reading

class Program

{

static void Main(string[] args)

{

using (StreamWriter sw = new StreamWriter("F:\\TestFile1.txt")) // file

creation

{

// Add some text to the file.

sw.Write("This is the ");

sw.WriteLine("header for the file. is a good boy");sw.WriteLine("-------------------");

// Arbitrary objects can also be written to the file.

sw.Write("The date is: ");

sw.WriteLine(DateTime.Now);

}

Page 2: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 2/32

using (StreamReader sr = new StreamReader("F:\\TestFile1.doc"))

{

String line;

// Read and display lines from the file until the end of 

// the file is reached. while ((line = sr.ReadLine()) != null)

{

Console.WriteLine(line);

}

} }

Page 3: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 3/32

Appending the text

class Program {

static void Main(string[] args)

{

string path = @"F:\prac\z3.txt";

// This text is added only once to the file.

if (!File.Exists(path))

{

// Create a file to write to.

using (StreamWriter sw = File.CreateText(path))

{

sw.WriteLine("Hello");

sw.WriteLine("And");

sw.WriteLine("Welcome");

}

}

Page 4: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 4/32

// This text is always added, making the file longer over time

// if it is not deleted.

using (StreamWriter sw = File.AppendText(path))

{ sw.WriteLine("This");

sw.WriteLine("is Extra");

sw.WriteLine("Text");

}

// Open the file to read from.

using (StreamReader sr = File.OpenText(path))

{

string s = "";

while ((s = sr.ReadLine()) != null)

{ Console.WriteLine(s);

}

}

}

Page 5: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 5/32

using System.IO;

class Program

{

static void Main()

{ // // Create new folder in C:\ volume. //

Directory.CreateDirectory("C:\\newfolder"); // // Create

another directory with different string literal syntax. //

Directory.CreateDirectory(@"C:\newfolder2"); // // } 

R esult of program There are 2 folders on your C:\ drive: 1.

newfolder 2. newfolder2

Page 6: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 6/32

Directory using System.IO;

namespace dir

{

class Program

{ static void Main(string[] args)

{

if (!Directory.Exists("D:\\Dir1"))

Directory.CreateDirectory("D:\\Dir1");

if (!File.Exists("Dir1\\File1.txt"))

File.Create("Dir1\\File1.txt");

Console.Write("Enter text :: ");

string data = Console.ReadLine(); File.WriteAllText("D:\\Dir1\\File1.txt", data);

string data1 = File.ReadAllText("D:\\Dir1\\File1.txt");

Console.WriteLine(data1);

}

}

}

Page 7: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 7/32

Write data

namespace ConsoleApplication2

{

class Program

{

static void Main(string[] args)

{

if (!File.Exists("E:\\test123.txt"))

File.Create("E:\\test123.txt");

string FileName = "E:\\test123.txt";

Console.WriteLine("Adding access control entry for " + FileName);

Console.Read();

// Add the access control entry to the File.

AddFileSecurity(FileName, "ast", FileSystemRights.WriteData,

AccessControlType.Deny);

Page 8: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 8/32

try

{

using (StreamWriter sw = File.AppendText(FileName)) {

sw.WriteLine("This");

sw.WriteLine("is Extra");

sw.WriteLine("Text");

} }

catch (Exception)

{

Console.WriteLine("trying to attempt write data");

}

}

Page 9: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 9/32

public static void AddFileSecurity(string FileName, string account,

FileSystemRights rights, AccessControlType controlType)

{

// Get a FileSecurity object that represents the

// current security settings.

FileSecurity fSecurity = File.GetAccessControl(FileName);

// Add the FileSystemAccessRule to the security settings. fSecurity.AddAccessRule(new FileSystemAccessRule(account,

rights, controlType));

// Set the new access settings.

File.SetAccessControl(FileName, fSecurity);

}

} }

Page 10: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 10/32

File security on read data using System.IO;

using System.Security.AccessControl;

namespace file_secon_read

{

class Program

{ static void Main(string[] args)

{

if (!File.Exists("E:\\test123.txt"))

File.Create("E:\\test123.txt");

string FileName = "E:\\test123.txt"; Console.WriteLine("Adding access control entry for "

+ FileName);

// Add the access control entry to the File.

AddFileSecurity(FileName, "ast",

FileSystemRights.ReadData, AccessControlType.Deny); }

Page 11: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 11/32

public static void AddFileSecurity(string FileName, string account,

FileSystemRights rights, AccessControlType controlType)

{

// Get a FileSecurity object that represents the

// current security settings.

FileSecurity fSecurity = File.GetAccessControl(FileName);

// Add the FileSystemAccessRule to the security settings.

fSecurity.AddAccessRule(new FileSystemAccessRule(account,

rights, controlType));

// Set the new access settings.

File.SetAccessControl(FileName, fSecurity);

}

} }

Page 12: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 12/32

Add or remove security using System.IO;

using System.Security.AccessControl; namespace ConsoleApplication3

{

class Program

{

static void Main(string[] args)

{

if (!File.Exists("E:\\test15.txt"))

File.Create("E:\\test15.txt");

string FileName = "E:\\test15.txt";

Console.WriteLine("Adding access control entry for "

+ FileName);

// Add the access control entry to the File.

Page 13: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 13/32

AddFileSecurity(FileName, "ast",

FileSystemRights.WriteData, AccessControlType.Deny);

Console.Read();

Console.WriteLine("Removing access control entry from "

+ FileName);

// Remove the access control entry from the File.

RemoveFileSecurity(FileName, "ast", FileSystemRights.WriteData,

AccessControlType.Deny); Console.WriteLine("after remove call");

Console.ReadLine();

}

Page 14: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 14/32

public static void AddFileSecurity(string FileName, string account,

FileSystemRights rights, AccessControlType controlType)

{

// Get a FileSecurity object that represents the

// current security settings.

FileSecurity fSecurity = File.GetAccessControl(FileName);

// Add the FileSystemAccessRule to the security settings.

fSecurity.AddAccessRule(new FileSystemAccessRule(account,

rights, controlType));

// Set the new access settings.

File.SetAccessControl(FileName, fSecurity);

}

public static void RemoveFileSecurity(string FileName, string account,

FileSystemRights rights, AccessControlType controlType)

{ // current security settings.

FileSecurity fSecurity = File.GetAccessControl(FileName);

Page 15: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 15/32

// Remove the FileSystemAccessR ule from the security settings.

fSecurity.RemoveAccessR ule(new FileSystemAccessR ule(account, rights,

controlType));

// Set the new access settings.

File.SetAccessControl(FileName, fSecurity);

}

}

}

Page 16: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 16/32

Generic

Generic allow the programmer to design classes or f unction

member and postpone the definition of types until the class is

instantiated.

Types are open type (it can be any type like int, string , bool)

Reusing of code

Page 17: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 17/32

Generic

namespace generic

{

class Program

{

static void swap<T>(ref T a, ref T b)

{

T temp;

temp = a; a = b;

 b = temp;

}

static void Main(string[] args)

{

int x=Convert.ToInt16(Console.ReadLine());

int y = Convert.ToInt16(Console.ReadLine());

Console.WriteLine(" val before {0},{1}",x,y);

swap<int>(ref x, ref y);

Page 18: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 18/32

Console.WriteLine(" val after {0},{1}", x, y);

string m = Console.ReadLine();

string n = Console.ReadLine();

swap<string>(ref m, ref n); Console.WriteLine(" val after {0},{1}", m, n);

}

}

}

Page 19: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 19/32

Generic delegate

namespace generic1

{

class Program

{

static void Main(string[] args)

{

m<int, string> m1 = new m<int, string>();

m<int, string>.d d1 = new m<int, string>.d(m1.mymethod); d1(911,"jon");

}

}

class m<T1, T2>

{

public delegate void d(T1 n, T2 m);

public void mymethod(T1 id, T2 name)

{

Console.WriteLine("{0}, {1}", id, name);

}

}

Page 20: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 20/32

Enumeration

It is a value type used to store a set of named constants such as

days of week, months etc.

Each enumeration has base type that can be one of integral type.

class Program

{ enum Importance

{

None,

Trivial,

Regular, Important,

Critical

};

Page 21: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 21/32

class Program

{ static void Main()

{ B b1 = B.Dog;

V v1 = V.Hidden;

Console.WriteLine(b1);

Console.WriteLine(v1);

}

enum V { None,

Hidden = 2,

Visible = 4

}; enum B

{ None,

Cat = 1,

Dog = 2

};

}

Page 22: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 22/32

class Program

{

enum week { sun, mon, tue, wed }; enum season { summer = 1, winter, spring };

enum grades { pass = 65, good = 75, verygood = 85,

distinct = 100 };

static void Main(string[] args) {

int sun1 = (int)week.sun;

int sum=(int)season.spring;

int g = (int)grades.verygood; Console.WriteLine("{0},{1},{2}", sun1, sum, g);

}

}

Page 23: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 23/32

Overloaded Methods

A type may overload methods, i.e. provide multiple methods with

the same name

Each must have a unique signature and Signature is based u pon

arguments only, the return value is ignored.

void Print(int i);

void Print(string s);

void Print(char c);

void Print(float f);

int Print(float f); // Error: duplicate signature

Page 24: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 24/32

Operator Overloading

Overloadable binary operators

+ - * / ! ~

% & | ^ == !=

  << >> < > <= >=

Page 25: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 25/32

Operator Overloading

No overloading for assignment operators, nor

these operators: sizeof, is, as, &&, ||.

Page 26: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 26/32

Operator Overloading namespace @operator 

{ class Program

{

static void Main(string[] args)

{

Complex num1 = new Complex(2, 3);

Complex num2 = new Complex(3, 4);

Complex sum = num1 + num2;

Console.WriteLine("First complex number: {0}", num1);

Console.WriteLine("Second complex number: {0}", num2);

Console.WriteLine("The sum of the two numbers: {0}", sum);

}

}

class Complex

{

 pu blic int real;

 pu blic int imaginary;

Page 27: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 27/32

public Complex(int real, int imaginary)

{ this.real = real;

this.imaginary = imaginary;

}

public static Complex operator +(Complex c1, Complex c2)

{ return new Complex(c1.real + c2.real, c1.imaginary +

c2.imaginary);

}

public override string ToString()

{ return (String.Format("{0} + {1}i", real, imaginary));

}

}

}

Page 28: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 28/32

is operator

The is operator is used to dynamically test if the r un-time typeof an object is compatible with a given type

public static void Test (object o)

{ if (o is Class1)

{

Console.WriteLine ("o is Class1");

}

else if (o is Class2)

{ Console.WriteLine ("o is Class2");

}

Page 29: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 29/32

namespace ConsoleApplication94 {

class Program

{

static void Main(string[] args)

{

Console.WriteLine("{0:C}", 1.2);

Console.WriteLine("{0:C}", -1.2);

Console.WriteLine("{0:D5}", 12);

Console.WriteLine("{0:F3}", 1.2); Console.WriteLine("{0:G}", 1.2);

Console.WriteLine("{0:X}", 123);

}

}

}

Page 30: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 30/32

Static Constructor

A static constructor is used to initialize any static data, or toperform a particular action that needs performed once only. It is

called automatically before the first instance is created or any

static members are referenced.

A static constructor does not take access modifiers or have

parameters.

public class Bus {

static Bus() {

System.Console.WriteLine("The static constructor invoked."); }

public static void Drive()

{ System.Console.WriteLine("The Drive method invoked."); } } class

TestBus

{static void Main() { Bus.Drive(); } }

Page 31: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 31/32

Private constructor

 pu blic class Counter 

{  private Counter()

{

 pu blic static int currentCount;

 pu blic static int IncrementCount()

{ return ++currentCount; } 

}

class TestCounter 

{ static void Main() { Counter aCounter = new Counter();

Counter.currentCount = 100;

Counter.IncrementCount();

System.Console.WriteLine("New count: {0}", Counter.currentCount); } }

Page 32: 15812_file security1

8/3/2019 15812_file security1

http://slidepdf.com/reader/full/15812file-security1 32/32

Copy Constructor class Person

{ private string name; private int age;

public Person(Person previousPerson)

{

name = previousPerson.name;

age = previousPerson.age;

}

public Person(string name, int age)

{ this.name = name; this.age = age; }

public string Details

{ get

{ return name + " is " + age.ToString(); } } }

class TestPerson

{ static void Main()

{ // Create a new person object. Person person1 = new Person("George", 40); // Create

another new object, copying person1. Person person2 = new Person(person1);

System.Console.WriteLine(person2.Details); } }