Προγραμματισμός Windows Κεφάλαιο 3 : The .NET Framework Library (FCL)

download Προγραμματισμός  Windows Κεφάλαιο  3 :  The .NET Framework Library (FCL)

of 35

  • date post

    30-Dec-2015
  • Category

    Documents

  • view

    21
  • download

    1

Embed Size (px)

description

.gr. Προγραμματισμός Windows Κεφάλαιο 3 : The .NET Framework Library (FCL). Μέρη έχουν παρθεί από το αγγλικό presentation του καθηγητή Dr. Juan Vargas για το μάθημα CSCE 547 του Department of Computer Science and Engineering University of South Carolina Columbia, SC 29208. - PowerPoint PPT Presentation

Transcript of Προγραμματισμός Windows Κεφάλαιο 3 : The .NET Framework Library (FCL)

  • Windows 3: The .NET Framework Library (FCL) presentation Dr. Juan Vargas CSCE 547 Department of Computer Science and EngineeringUniversity of South CarolinaColumbia, SC 29208

    .gr

  • 7,000 (Classes, structs, interfaces, enumerations and delegates) FCL. . FCL .

    FCL 100 namespaces. : I/O (File and Stream) Collections (Hash Tables and Dynamic Arrays) Regular Expressions Internet Classes (HttpWebRequest, HttpWebResponse, System.WebMail) Data (DataReadears, DataSets, Data Adapters) Reflection

  • 30 classes, 1 struct, 3 delegates 7 enums System.IO namespace. classes BinaryReader, BinaryWriter, BufferedStream, Directory, DirectoryInfo, File, FileInfo, FileStream, FileSystemInfo, IOException, MemoryStream, Path, Stream, StreamWriter, StreamReader, TextReader, TextWriter. , IO classes namespaces, .. NetworkStream namespace System.Net.Sockets.

    I/O: FileStream object. , instances BinaryReader BinaryWriter FileStream object BinaryReader BinaryWriter Read Write. / , StreamReader StreamWriter FileStream object StreamReader StreamWriter ReadLine WriteLine. (Close) FileStream object.

  • using System; using System.IO;class MyApp { static void Main (string[] args) { // Make sure a file name was entered on the command line if (args.Length == 0) {Console.WriteLine ("Error: Missing file name"); return; }// Open the file and display its contentsStreamReader reader = null; try {reader = new StreamReader (args[0]); for (string line = reader.ReadLine (); line != null; line = reader.ReadLine reader.ReadLine ()) Console.WriteLine (line); }catch (IOException e) { Console.WriteLine (e.Message); }finally { if (reader != null)reader.Close (); } } }

  • // Use File.Open to create a FileStream, and then wrap a // StreamReader around it FileStream stream = File.Open (filename, FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader (stream);

    // Create a FileStream directly, and then wrap a // StreamReader around it FileStream stream = new FileStream (filename, FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader (stream);

    // Use File.OpenText to create a FileStream and a // StreamReader in one stepStreamReader reader = File.OpenText (filename);

  • . , . void LogException (string filename, Exception ex) {StreamWriter writer = null; try {writer = new StreamWriter (filename, true);writer.WriteLine (ex.Message); }finally { if (writer != null)writer.Close (); } } try finally .

  • T BinaryReader BinaryWriter classes . scramble , . : Scramble iFile passwordO . ASCIIEncoding enc = new ASCIIEncoding ();byte[] keybytes = enc.GetBytes (key);

  • using System; using System.IO; using System.Text;class MyApp { const int bufsize = 1024;

    static void Main (string[] args) { if (args.Length < 2) { Console.WriteLine (" Syntax: SCRAMBLE filename key"); return; }

    string filename = args[0]; string key = args[1]; FileStream stream = null; try {stream = File.Open (filename, FileMode.Open, FileAccess.ReadWrite);

    BinaryReader reader = new BinaryReader (stream);BinaryWriter writer = new BinaryWriter (stream);

    ASCIIEncoding enc = new ASCIIEncoding ();byte[] keybytes = enc.GetBytes (key);

    byte[] buffer = new byte[bufsize]; byte[] keybuf = new byte[bufsize + keybytes.Length - 1];

    int count = (1024 + keybytes.Length - 1) / keybytes.Length; for (int i=0; i 0) { long lPosition = stream.Position; int nBytesRequested = (int) System.Math.Min (bufsize, lBytesRemaining); int nBytesRead = reader.Read (buffer, 0, nBytesRequested);

    for (int i=0; i

  • Collections weakly typed , System.Object. casts objects. collections CollectionBase DictionaryBase classes.

    . 2.0 Generics. Generics classes, structures, interfaces methods . generics class : public class Generic { public T Field;} instance class, T:Generic g = new Generic();g.Field = "A string"; . 2.0 class Collection, collection.

  • Hash tables key/values buckets. hash tables . , buckets.

    HashTables :HashTable aTable = new HashTable( );aTable.Add( Sunday, Domingo); // (key,value) pairaTable[Monday] = Lunes; // table[Key]=value;foreach (DictionaryEntry entry in table)Console.Write(Key={0}, Value={0}\n, entry.Key, Entry.Value);aTable.Remove(key); aTable.Clear(); aTable.Count(); ...

  • arrays .NET:

    Array class System.Object. class ( ) arrays . element () Array. length elements array. rank Array . Array Array. Array . Type objects array type. Array objects Array type Type object. Array 6 (IsFixedSize, IsReadOnly, IsSynchronized,Length, Rank, SyncRoot), 22 ( BinarySearch, Clear, Copy, Equals,GetValue, GetLength, IndexOf, Initialize, Reverse, SetValue, Sort, ToString,Finalize ..). class IList, ( Add, Clear, Contains, IndexOf, Insert, Remove, RemoveAt ).

  • H ArrayList class System.Collections. class class System.Object.Array. class :[Serializable Serializable]public class ArrayList : IList, ICollection, IEnumerable, ICloneable class:ArrayList aList = new ArrayList ();aList.Add ("John"); aList.Add ("Paul"); aList.Add ("George"); :ArrayList list = new ArrayList (100000);for (int i=0; i
  • WordCount I/O, Array, HashTable, SortedList, ArrayList. :wordcount iFile

    StreamReader reader = null; Hashtable table = new Hashtable ();Try {reader = new StreamReader (args[0]);for (string line=reader.ReadLine; line!=null;line=reader.ReadLine()) {string[] words = GetWords (line);foreach (string word in words) {string iword = word.ToLower ();if (table.ContainsKey (iword))table[iword] = (int) table[iword] + 1;elsetable[iword] = 1;}}SortedList list = new SortedList (table);Console.WriteLine ("{0} unique words in {1}", table.Count, args[0]);foreach (DictionaryEntry entry in list)Console.WriteLine ("{0} ({1})",entry.Key, entry.Value);}

  • static string[] GetWords (string line) {ArrayList al = new ArrayList ();int i = 0;string word;char[] characters = line.ToCharArray ();while ((word = GetNextWord (line, characters, ref i)) != null)al.Add (word);// Return static array equivalent to the ArrayListstring[] words = new string[al.Count];al.CopyTo (words);return words; }

    static string GetNextWord (string line, char[] characters, ref int i) {// Find the beginning of the next wordwhile (i < characters.Length && ! Char.IsLetterOrDigit (characters[i]))i++;if (i == characters.Length)return null;int start = i;// Find the end of the wordwhile (i < characters.Length && Char.IsLetterOrDigit (characters[i]))i++;// Return the wordreturn line.Substring (start, i - start);}Operate on ArayList and Convertto string[] prior to return

  • .cs, :

    113 unique words found in class1.c

    ... ... ... .

  • RegEx class name space System.Text.RegularExpressions. RegEx : strings substrings, RegExps . substrings strings, RegEx (patterns). - RegEx . HTML.

    RegEx objects, pattern constructor:Using System.Text.RegularExpressions;Regex regex = new Regex ("[a-z]");Regex regex = new Regex ("[a-z]", RegexOptions.IgnoreCase);

    Regex regex = new Regex (@"\\");string[] parts = regex.Split (@"c:\inetpub\wwwroot\wintellect");foreach (string part in parts) Console.WriteLine (part);

    c:inetpubwwwrootwintellect (@\) compiler substrings string RegExp

  • html, , ..,Everygoodboydoesfine

    Regex regex = new Regex ("]*>");string[] parts =regex.Split ("Everygoodboydoesfine");foreach (string part in parts) Console.WriteLine (part); :Every good boy does fine ( ). ]*> : , >.

    To Regex 3 substrings strings: Match, Matches, IsMatch. , grep-like. greps, ..NetGrep index.html ]*> index.html. , NetGrep file1.txt \d{2,} file1.txt, ...

  • using System; using System.IO; using System.Text.RegularExpressions;

    class MyApp { static void Main (string[] args) { if (args.Length < 2) { Console.WriteLine ("Syntax: NETGREP filename expression"); return; }StreamReader reader = null; int linenum = 1; try {// Initialize a Regex object with the regular expression // entered on the command lineRegex regex = Regex (args[1], RegexOptions.IgnoreCase);// Iterate through the file a line at a time and display all lines that contain a pattern matching the regular expression reader = new StreamReader (args[0]); for (string line = reader.ReadLine (); line != null; line = reader.ReadLine (), linenum++) { if (regex.IsMatch (line)) Console.WriteLine ("{0:D5}: {1}", linenum, line);} }catch (Exception e) { Console.WriteLine (e.Message); }finally { if (reader != null) reader.Close ();}}}

  • classes name space System.Net. 36 classes, 5 interfaces, delegate, 4 enumerations.

    classes : Cookie, CookieCollection,Dns, FileWebRequest, FileWebResponse, HttpVersion,HttpWebRequest, HttpWebResponse, IPAddress, IPEndPoint,IPHostEntry, NetworkCredential, ProtocolViolationException,SocketAddress, WebClient, WebException, WebPermission,WebProxy, WebRequest, WebResponse .

    WebRequest WebResponse LinkList.cs, URL . URL websites, .. . .

  • using System.IO; using System.Net; using System.Text.RegularExpressions;

    class MyApp { static void Main (string[] args) { if (args.Length == 0) { Console.WriteLine ("Error: Missing URL"); return; }