學(xué)習(xí)筆記系列_c_第1頁(yè)
學(xué)習(xí)筆記系列_c_第2頁(yè)
學(xué)習(xí)筆記系列_c_第3頁(yè)
學(xué)習(xí)筆記系列_c_第4頁(yè)
學(xué)習(xí)筆記系列_c_第5頁(yè)
已閱讀5頁(yè),還剩27頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、學(xué)習(xí)筆記系列C#學(xué)習(xí)筆記夜晚回家2010/6/141目錄C#中關(guān)鍵字的小提示 如何產(chǎn)生隨機(jī)數(shù) 反射 - 獲取運(yùn)行時(shí)方法的信息在 C#中使用 Params 關(guān)鍵字利用正則表達(dá)式統(tǒng)計(jì)單詞個(gè)數(shù)11133445689(Label)隨鼠標(biāo)移動(dòng)而移動(dòng)如何從你的應(yīng)用程序執(zhí)行 exe 文件在控制臺(tái)應(yīng)用程序中隱藏用戶(hù)的在 頁(yè)面上實(shí)現(xiàn) a.Equals(b) 和 a =b 之間的區(qū)別 使用 ArrayList 創(chuàng)建和使用 HashTable10101214151920202425262729鍵值對(duì)的集合 SortedList:棧:先進(jìn)后出隊(duì)列 隊(duì)列:先進(jìn)先出列表 在 C#中遍歷和排序?qū)ο蠹?StringWrit

2、er 實(shí)現(xiàn)的一個(gè)功能 創(chuàng)建一個(gè)文本文件并寫(xiě)入文本序列化對(duì)象到文件 文件夾拷貝 使用 C#簡(jiǎn)單 XML 讀 XML 文件用 XmlWriter 寫(xiě) XML 文件 數(shù)據(jù)到 XML 文檔 C#中關(guān)鍵字的小提示C#不允許使用關(guān)鍵字作為標(biāo)識(shí)符。雖然使用關(guān)鍵字作為標(biāo)識(shí)符名稱(chēng)是不可取的但有時(shí)使用其他語(yǔ)言的代碼可能會(huì)需要使用關(guān)鍵字作為標(biāo)識(shí)符。在這種情況下可以在關(guān)鍵字前面加一個(gè) 符號(hào)用在標(biāo)識(shí)符。在上面的示例中定義了一個(gè)變量名為string 的字符串變量和一個(gè)方法名為void 的方法。參考地址:/index.php?topic=1702.0如何產(chǎn)生隨機(jī)數(shù)參考地址:/index.php?topic=1703.0反射

3、 - 獲取運(yùn)行時(shí)方法的信息1using System;using System.Reflection;class ThisClassint firstValue, secondValue; public int sum()return firstValue + secondValue;public void assign(int a, int b)Random rand = new Random();/ 產(chǎn)生一個(gè) 10 到 20 之間的隨機(jī)數(shù)int num1 = rand.Next( 10, 20 );/ 產(chǎn)生一個(gè)小于 100 的隨機(jī)數(shù)int num2 = rand.Next( 100 );u

4、sing System; class Teststatic void Main()string string=Mystring;void void() Console.WriteLine(Void Method);下面代碼是 GetMethods()的重載方法:說(shuō)明DeclaredOnly = 指定只應(yīng)考慮在所提供類(lèi)型的層次結(jié)構(gòu)級(jí)別上聲明的成員。Instance = 指定實(shí)例成員。Public = 指定公共成員。參考地址:/index.php?topic=1720.02MethodInfo methods =t.GetMethods(BindingFlags.DeclaredOnly | Bi

5、ndingFlags.Instance | BindingFlags.Public );firstValue = a; secondValue = b;class ReflectionDemopublic static void Main()Type t = typeof( ThisClass );/get a Type object representing ThisClass. Console.WriteLine( Analyzing methods in +t.Name ); Console.WriteLine( nMethods supported: n );MethodInfo me

6、thods = t.GetMethods();/Obtains List of methods. foreach( MethodInfo m in methods )Console.Write( . +m.ReturnType.Name+ +m.Name + ( ); ParameterInfo parameters = m.GetParameters();/Obtains list of parameters.for ( int i = 0; i parameters.Length; i+ ) Console.Write( parametersi.ParameterType.Name+ +p

7、arametersi.Name ); if( i + 1 0)Console.Write(b0b); sb.Length-;continue;Console.Write(*); sb.Append(cki.KeyChar);Console.WriteLine(Your password is: 0, sb.ToString();在 頁(yè)面上實(shí)現(xiàn)在頁(yè)面上實(shí)際上就是在通過(guò)“Windows Media Player”來(lái)。在 apsx頁(yè)面上放一個(gè) Lable 控件,這種方式可以寫(xiě) Javascript 等控制頁(yè)面,和布局頁(yè)面。HTML 頁(yè)面.cs 頁(yè)面代碼6protectedvoidPage_Load(

8、objecder,EventArgse) if(!this.IstBack) stringfilename = jbsong2.mpg;/要的的名稱(chēng) - 注意路徑的正確寫(xiě)法this.lbl_MediaPlayer.Text = this.PlayMedia(filename);publicstringPlayMedia(stringMediaFile) stringstrScript =+ 多頁(yè)面 7+ +說(shuō)明是為了加載 Windows Media Player 控件。其中22D6F312-B0F6-11D0-94AB-0080C74C7E95 是 Windows Media Player 在

9、Windows 系統(tǒng)中 cla。通過(guò) PARAM 來(lái)設(shè)置 Windows Media Player 的各個(gè)屬性,其中 Name 后面的是屬性名, Value 后面的是屬性的值。參考地址:/index.?topic=628.0a.Equals(b) 和a = b 之間的區(qū)別.Equals 和 = 都是用于比較。如果 a 和 b 都是值類(lèi)型,則 a.Equals(b) 和 a = b 結(jié)果相同,但是在們的行為是不同的:類(lèi)型是它8string a = new string(new char d, a, n, c, e);string b = new string(new char d, a, n,

10、c, e);+ + + ;returnstrScript;輸出結(jié)果:參考地址:/index.php?topic=1988.0使用 ArrayListArrayList 可以保存各種類(lèi)型的對(duì)象。可以將整型、字符串甚至包括DataTable 等復(fù)雜數(shù)據(jù)類(lèi)型。創(chuàng)建一個(gè) ArrayList 并添加 4 中不同類(lèi)型的項(xiàng)DataSet、迭代整個(gè) ArrayList 逐一讀取數(shù)據(jù)。我們添加的 4 個(gè)項(xiàng)是不同數(shù)據(jù)類(lèi)型。因此,需要檢查每個(gè)項(xiàng)的數(shù)據(jù)類(lèi)型并顯示。9/ Now retrieve items one by one. foreach (object item in myList)if ( item.Get

11、Type() = typeof(string) ) Console.WriteLine( Item is a string. );else if ( item.GetType() = typeof(int) ) Console.WriteLine( Item is a integer. );else if ( item.GetType() = typeof(DateTime) ) Console.WriteLine( Item is a DateTime object. );else if ( item.GetType() = typeof(DataTable) ) Console.Write

12、Line( Item is a DataTable. );ArrayList myList = new ArrayList();myList.Add (DotNetSpider);/ Add a string. myList.Add(1032);/ Add an integer myList.Add( DateTime.Now ); / Add current time. myList.Add( new DataTable() ); / Add a datatableTrue True False Trueobject c = a; object d = b;Console.WriteLine

13、 (a=b); Console.WriteLine (a.Equals(b); Console.WriteLine (c=d); Console.WriteLine (c.Equals(d);上面代碼結(jié)果顯示:參考地址:/index.?topic=1698.0創(chuàng)建和使用 HashTableHashTable鍵值對(duì)的數(shù)據(jù)。當(dāng)有許多數(shù)據(jù)需要通過(guò)鍵來(lái)查找是使用 HashTable 是非常有效的。下面的示例用于顯示如何為 HashTable 添加鍵值對(duì)。使用鍵索引獲取值:參考地址:/index.?topic=1692.0鍵值對(duì)的集合SortedList:創(chuàng)建一個(gè) SortedList 機(jī)會(huì)用于基于鍵上

14、值排序的鍵值對(duì)。可以通過(guò)如下屬性獲得 SortedList 的只讀的 keys 和 values 值。下面是 SortedList 的演示程序:10/ SortedList 示例using System;using System.Collections;class SortedListDemo public virtual ICollection Keys get; public virtual ICollection Values get;string val = tableJohn.ToString();Hashtable table = new Hashtable();table.Add

15、(John, 2003);table.Add(Tom, 1975);table.Add(Teena, 1988);Item is a string. Item is aeger.Item is a DateTime object. Item is a Daable.程序輸出:11Contents of the list through indexers. Book: WrittenPumpkin: Orange Rose: Redsky: BlueWater: TransparentContents by means of theeger indexes.public sicvoidMain(

16、) /創(chuàng)建SortedList.SortedListsortedList = newSortedList();/添加元素sortedList.Add(sky,Blue);sortedList.Add(Rose,Red);sortedList.Add(Water,Transparent);sortedList.Add(Pumpkin,Orange);/添加使用索引sortedListBook = Written;/獲取keys 集合ICollectioniCollection = sortedList.Keys;/使用keys 獲取 valuesConsole.Wriine(Contentsof

17、thelistthroughindexers.);foreach(stringstriniCollection)Console.Wriine(str + : + sortedListstr);Console.Wriine();/使用整型所以顯示列表Console.Wriine(Contentsbymeansoftheegerindexes.);for(i = 0; i );int number = (int) stack.Pop(); Console.WriteLine(number); Console.Write(stack: ); foreach(int i in stack)Writte

18、n Document OrangeRed BlueTransparentInteger indexes of all the Entries. Book: 0Pumpkin: 1Rose: 2sky: 3Water: 4程序輸出如下。注意如何處理 InvalidOperationException 異常來(lái)管理?xiàng)O乱纭⒖嫉刂罚?index.php?topic=1705.013Push(2) stack: 2 Push(4) stack: 4 2 Push(77) stack: 77 4 2Pop - 77stack: 4 2Pop - 4stack: 2Pop -2 stack:Pop - S

19、tack empty.Console.Write(i + ); Console.WriteLine();public static void Main() Stack stack = new Stack();foreach(int i in stack) Console.Write(i + );Console.WriteLine();ShowPush(stack,2); ShowPush(stack,4); ShowPush(stack,77); ShowPop(stack); ShowPop(stack); ShowPop(stack);tryShowPop(stack);catch(Inv

20、alidOperationException) Console.WriteLine(Stack empty.); Console.Read();隊(duì)列:先進(jìn)先出列表這篇文章告訴你如何使用隊(duì)列。調(diào)用 Enqueue()將一個(gè)對(duì)象放入隊(duì)列中。Dequeue()方法移除并返回隊(duì)列中最前面的對(duì)象。當(dāng)隊(duì)列是空的時(shí)候調(diào)用 Deueue() 方法會(huì)引發(fā)InvalidOperationException 異常。使用對(duì)象。Peek()可以返回下一個(gè)對(duì)象,但不移除該下面是關(guān)于隊(duì)列的示例:14/ 顯示Queue 類(lèi)using System;using System.Collections;class QueueDem

21、ostatic void ShowEnqueue(Queue queue, int number) queue.Enqueue(number); Console.WriteLine(Enqueue( + number + ); Console.Write(queue: );foreach(int i in queue) Console.Write(i + );Console.WriteLine();static void ShowDequeue(Queue queue) Console.Write(Dequeue - );int number = (int) queue.Dequeue();

22、Console.WriteLine(number); Console.Write(queue: ); foreach(int i in queue)Console.Write(i + ); Console.WriteLine();public static void Main() Queue queue = new Queue();foreach(int i in queue) Console.Write(i + );Console.WriteLine();ShowEnqueue(queue,1); ShowEnqueue(queue,5); ShowEnqueue(queue,90); Sh

23、owDequeue(queue); ShowDequeue(queue);輸出結(jié)果為:參考地址:/index.php?topic=1704.0在 C#中遍歷和排序?qū)ο蠹?5using System;using System.Collections;/Class Employee class Employee public string EmpName; public string Dept;public Employee(string szName, string szDept) EmpName = szName;Dept = szDept;Enqueue(1) queue: 1 Enque

24、ue(5) queue: 1 5 Enqueue(90) queue: 1 5 90Dequeue - 1queue: 5 90Dequeue - 5queue: 90Dequeue - 90 queue:Dequeue - Queue empty.ShowDequeue(queue);tryShowDequeue(queue);catch(InvalidOperationException) Console.WriteLine(Queue empty.); Console.Read();/Declaretheclass to beimplementIEnumerable /Employees

25、 lass ontains he mployees ist nd numeration ill esult n oing thruemployees classEmployees /Declare a member of EmployeespublicArrayList oEmployees;/ConstructorforEmployeespublicEmployees() oEmployees = newArrayList();/Declare a enumeratormemberclass EnumEmployee : IEnumerator /Declarea variable to h

26、oldcurrentindex/Declarea variable of Enumerableclasst isEmployeepublicCurrentIndex;publicEmployeesoEmpList;/ConstructorpublicEnumEmployee(EmployeesoEmps) oEmpList = oEmps;CurrentIndex = -1;/Declare a methodto get thecurrentemployee/Mandatorily implemented.DontchangethenamepublicobjectCurrent get ret

27、urnoEmpList.oEmployeesCurrentIndex;/Handlemovenext : Mandatorilyimplemented.Dontchangethenamepublicbool MoveNext() if(CurrentIndex oEmpList.oEmployees.Count - 1) CurrentIndex+;returntrue; elsereturnfalse;/Reset the variable : Mandatorilyimplemented.Dontchangethenamepublicvoid Reset() 1617CurrentInde

28、x = -1;/ClassforComparison/MemberforsortorderpublicenumEmployeeSortOrder eSortNameDesc,eSortNameAsc,eSortDeptDesc,eSortDeptAsc;classEmployeesSort : IComparer /SortorderprivateSortOrder;/ConstructorpublicEmployeesSort(Sort) SortOrder = Sort;/ImplementComparefunctionpublicCompare(objectoEmployee1,obje

29、ctoEmployee2) switch(SortOrder) case ()EmployeeSortOrder.eSortNameDesc:return(Employee)(oEmployee2)pareTo(Employee)(oEmployee1).EmpName);case ()EmployeeSortOrder.eSortNameAsc:return(Employee)(oEmployee1)pareTo(Employee)(oEmployee2).EmpName);case ()EmployeeSortOrder.eSortDeptDesc:return(Employee)(oEm

30、ployee2)pareTo(Employee)(oEmployee1).Dept);case ()EmployeeSortOrder.eSortDeptAsc:return(Employee)(oEmployee1)pareTo(Employee)(oEmployee2).Dept);default:return(Employee)(oEmployee1)pareTo(Employee)(oEmployee2).EmpName);/Write a functiontogettheenumeratorpublicIEnumeratetEnumerator() returnnewEnumEmpl

31、oyee(this);參考地址:/index.php?topic=2007.018/Write a function to call sortpublic void SortEmployees(int SortType) oEmployees.Sort(new EmployeesSort(SortType);/Nowuse the enumeration :-) public static void Main() Employee oTemp;/Create the Employees class Employees oEmpList = new Employees();/Add Employ

32、ees to the oEmployees member oEmpList.oEmployees.Add(new Employee(Lalitha, SE); oEmpList.oEmployees.Add(new Employee(Lal, Associate); oEmpList.oEmployees.Add(new Employee(KLA, Executive);/Nowloop using foreach Console.WriteLine(Using ForEach); Console.WriteLine();foreach(Employee oEmp in oEmpList.oE

33、mployees) Console.WriteLine(oEmp.EmpName);/Sort the Employees list oEmpList.SortEmployees(1);/AlrightTry enumeration./Get the enumerator and start enumeration IEnumerator e = oEmpList.GetEnumerator();Console.WriteLine(nn Using Enumerator); Console.WriteLine(n);while(e.MoveNext() oTemp = (Employee)(e

34、.Current); Console.WriteLine(oTemp.EmpName + + oTemp.Dept); oTemp = null;StringWriter 實(shí)現(xiàn)的一個(gè)功能如何用一組雙倍間距的句子創(chuàng)建續(xù)的段落,然后將該段落重新轉(zhuǎn)換為原來(lái)的文本:19usingSystem; usingSystem.IO; classStringRW sicvoidMain() stringtextReaderText = TextReaderistheabstractbase +classofStreamReaderandStringReader,whichread +charactersfrom

35、streamsandstrings,respectively.nn +Createan instanceofTextReadertoopen a textfile +forreading a specifiedrangeofcharacters,orto +create a readerbasedonanexistingstream.nn +You csouseaninstanceofTextReadertoread +textfrom a custombackingstoreusingthesame +APIsyouwouldusefor a stringor a stream.nn;Con

36、sole.Wriine(Originaltext:nn0,textReaderText);stringaLine,aParagraph = null;StringReaderstrReader = newStringReader(textReaderText);while(true) aLine = strReader.ReadLine();if(aLine !=null) aParagraph = aParagraph + aLine + ; else aParagraph = aParagraph + n;break;Console.Wriine(Modifiedtext:nn0,aPar

37、agraph);Character;charconvertedCharacter;StringWriterstrWriter = newStringWriter();strReader = newStringReader(aParagraph);while(true) Character = strReader.Read();if(Character=-1)break;convertedCharacter = Convert.ToChar(Character);if(convertedCharacter=.) strWriter.Write(.nn);strReader.Read();strR

38、eader.Read(); elsestrWriter.Write(convertedCharacter);Console.Wriine(nOriginaltext:nn0,strWriter.ToString();參考地址:/index.?topic=1794.0創(chuàng)建一個(gè)文本文件并寫(xiě)入文本這里的 C#代碼示例創(chuàng)建并寫(xiě)入一個(gè)文本文件。參考地址:/index.?topic=1693.0序列化對(duì)象到文件信息在內(nèi)存中是非常好的,但有時(shí)你的用戶(hù)不得不關(guān)閉你的應(yīng)用程序。如果你想知道存儲(chǔ)在內(nèi)存中是什么數(shù)據(jù),也就意味著(可能)你有時(shí)需要把信息寫(xiě)入文件。今天,一個(gè).NET 內(nèi)置特征叫做序列化 從文件讀寫(xiě)數(shù)據(jù)非

39、常的容易。來(lái)看在這個(gè)例子中,創(chuàng)建一個(gè)程序所有有汽車(chē)的朋友。為了實(shí)現(xiàn)這個(gè)功能我創(chuàng)建了兩個(gè)車(chē)牌、型號(hào)和汽車(chē)使用年限。Owner 對(duì)象保存汽車(chē)擁對(duì)象:Car 和 Owner。Car 對(duì)象有者的一些信息。每個(gè) Car 對(duì)象包含一個(gè) Owner 對(duì)象的。20/關(guān)于 Car 的信息public class Car private string make; private string m; privateyear; private Owner owner; public Car() /關(guān)于汽車(chē)擁有者的信息 public class Owner System.IO.StreamWriter writer;w

40、riter = System.IO.File.CreateText(c:TestFile.txt); writer.Wriine(o file!);writer.Close();因?yàn)榭赡懿恢挂粋€(gè)朋友,需要?jiǎng)?chuàng)建一個(gè) Car 的列表對(duì)象。現(xiàn)在已經(jīng)創(chuàng)建了需要的對(duì)象了,接下去準(zhǔn)備序列化他們。當(dāng)我保存數(shù)據(jù)到文件,創(chuàng)建一個(gè)特殊對(duì)象用于保存序列化的所有東西。這個(gè)類(lèi)包含系列化的每個(gè)對(duì)象的。在這個(gè)例子中,我僅想保存汽車(chē)列表。現(xiàn)在讓我的對(duì)象。我通常創(chuàng)建一個(gè) Serializer 類(lèi)用于們創(chuàng)建一個(gè)函數(shù)用于序列化和反序列化控制文件的讀寫(xiě)。21using System.IO;using System.Runtime.S

41、erialization;using System.Runtime.Serialization.Formatters.Binary;public class Serializer public Serializer() public void SerializeObject(string filename, ObjectToSerialize objectToSerialize) Stream stream = File.Open(filename, FileMode.Create); BinaryFormatter bFormatter = new BinaryFormatter(); bF

42、ormatter.Serialize(stream, objectToSerialize); stream.Close();public ObjectToSerialize DeSerializeObject(string filename) ObjectToSerialize objectToSerialize;Stream stream = File.Open(filename, FileMode.Open); BinaryFormatter bFormatter = new BinaryFormatter();objectToSerialize = (ObjectToSerialize)

43、bFormatter.Deserialize(stream); stream.Close();public class ObjectToSerialize privaist cars; public List Cars get return this.cars; set this.cars = value; public ObjectToSerialize() List cars = new List();private stringName; private string lastName; public Owner() 正如你所看到的,序列化一個(gè)對(duì)象所需要的代碼實(shí)際上是非常小和簡(jiǎn)單的。在這

44、一點(diǎn)上,代碼沒(méi)有修改。在 Serialize Serializable 特性并且函數(shù)前可以調(diào)用 ObjectToSerialize,須實(shí)現(xiàn) ISerializable 接口。須包括作為 ISerializable 接口,這個(gè)類(lèi)必須包含其他構(gòu)造方法初始對(duì)象,并有一個(gè) GetObjectData 函數(shù)用于描述如何序列化對(duì)象。因此 Car 和 Owner 對(duì)象也被序列化,他們也需要實(shí)現(xiàn)這些方法。22Serializable()public class Car : ISerializable private string make;private string m; privateyear; priv

45、ate Owner owner;public Car() public Car(SerializationInfo info, StreamingContext ctxt) this.make = (string)info.GetValue(Make, typeof(string);using System.Runtime.Serialization;using System.Runtime.Serialization.Formatters.Binary; Serializable()public class ObjectToSerialize : ISerializable privaist

46、 cars;public List Cars get return this.cars; set this.cars = value; public ObjectToSerialize() public ObjectToSerialize(SerializationInfo info, StreamingContext ctxt) this.cars = (List)info.GetValue(Cars, typeof(List);public void GetObjectData(SerializationInfo info, StreamingContext ctxt) info.AddV

47、alue(Cars, this.cars);return objectToSerialize;現(xiàn)在,保存對(duì)象列表到文件,所需要做的是調(diào)用 Serializer 類(lèi)的 Serialize 和 DeSerialize 方法。23List cars = new List();/保存汽車(chē)列表到文件ObjectToSerialize objectToSerialize = new ObjectToSerialize(); objectToSerialize.Cars = cars;Serializer serializer = new Serializer(); serializer.Serialize

48、Object(outputFile.txt, objectToSerialize);/汽車(chē)列表以及被保存到outputFile.txt中/從outputFile.txt文件中信息objectToSerialize = serializer.DeSerializeObject(outputFile.txt); cars = objectToSerialize.Cars;this.m= (string)info.GetValue(M, typeof(string); this.year = ()info.GetValue(Year, typeof(); this.owner = (Owner)in

49、fo.GetValue(Owner, typeof(Owner);public void GetObjectData(SerializationInfo info, StreamingContext ctxt) info.AddValue(Make, this.make);info.AddValue(M, this.m); info.AddValue(Make, this.year); info.AddValue(Owner, this.owner);Serializable()public class Owner : ISerializable private stringName; pri

50、vate string lastName;public Owner() public Owner(SerializationInfo info, StreamingContext ctxt) this.Name = (string)info.GetValue(Name, typeof(string); this.lastName = (string)info.GetValue(LastName, typeof(string);public void GetObjectData(SerializationInfo info, StreamingContext ctxt) info.AddValu

51、e(Name, this.Name);info.AddValue(LastName, this.lastName);所有的這些都需要從二進(jìn)制文件中保存和加載 C#自定義對(duì)象。像所有文件一樣,你的這些文件也可能被破壞。因此,最好的想法是當(dāng)從文件讀取一個(gè)破壞的對(duì)象時(shí)執(zhí)行錯(cuò)誤處理。參考地址:/index.php?topic=2015.0文件夾拷貝這里附上文件夾拷貝,將文件夾從本地的一個(gè)地方拷貝到另一個(gè)地方。這里包含兩個(gè)按鈕用來(lái)讀取源路徑和目標(biāo)路徑。上面的代碼打開(kāi)一個(gè)文件選擇對(duì)話(huà)框請(qǐng)求一個(gè)路徑。最后一個(gè)按鈕用于從源位置拷貝到目標(biāo)位置。24private void btnMove_Click(objec

52、t sender, EventArgs e) try if(!String.IsNullOrEmpty(txtSource.Text) &!String.IsNullOrEmpty(txtDestination.Text) if(Directory.Exists(txtSource.Text) Directory.Move(txtSource.Text, txtDestination.Text);MessageBox.Show(文件夾移動(dòng)!);private void btnSource_Click(object sender, EventArgs e) string folderSelect

53、ed = GetFolderPath(); if(!String.IsNullOrEmpty(folderSelected)txtSource.Text = folderSelected;private void btnDestination_Click(object sender, EventArgs e) string folderSelected = GetFolderPath(); if(!String.IsNullOrEmpty(folderSelected)txtDestination.Text = folderSelected;private string GetFolderPa

54、th() FolderBrowserDialog fbd = new FolderBrowserDialog(); DialogResult result = fbd.ShowDialog();if(result = DialogResult.OK) return fbd.SelectedPath;return string.Empty;參考地址:/index.php?topic=1687.0使用 C#解析簡(jiǎn)單 XML這篇文章及代碼顯示如何使用.NET 中的 XML 有關(guān)類(lèi)庫(kù)解析 XML 文檔。這個(gè)應(yīng)用程序讀 XML 文件后根據(jù) xml 標(biāo)簽變量將值顯示到消息框。System.Xml 是需要加

55、入的命名空間,使用 using 添加。從創(chuàng)建 XmlDocument 類(lèi)的一個(gè)新實(shí)例開(kāi)始。接下去使用 load 方法從指定位置加載 XML 文檔。使用 GetElementsByTagName()方法獲得元素集中匹配特定名稱(chēng)的元素。XmlNodeList 顯示一個(gè)有序的 nodes 集合。在消息框中顯示結(jié)果。InnerText 將返回節(jié)點(diǎn)關(guān)聯(lián)的值和所有的子節(jié)點(diǎn)。參考地址:/index.php?topic=884.025MessageBox.Show(Name: + name0.InnerText);XmlNodeList name = xDoc.GetElementsByTagName(myN

56、ame); XmlNodeList age = xDoc.GetElementsByTagName(myAge);xDoc.Load(sampleXML.xml);XmlDocument xDoc = new XmlDocument(); elseMessageBox.Show(源或目標(biāo)不存在!, 錯(cuò)誤, MessageBoxButtons.OK, MessageBoxIcon.Error); elseMessageBox.Show(源或目標(biāo)不存在!, 錯(cuò)誤, MessageBoxButtons.OK, MessageBoxIcon.Error); catch(Exception ee) Me

57、ssageBox.Show(ee.Message, 錯(cuò)誤, MessageBoxButtons.OK, MessageBoxIcon.Error);讀 XML 文件XmlReader 和 XmlTextReader 類(lèi)被定義在 System.XML 命名空間。XmlTextReader 類(lèi)繼承自 XmlReader 類(lèi)。 XmlTextReader 類(lèi)用于文檔知道節(jié)點(diǎn)結(jié)束。XML 文檔。讀這篇文章,將顯示如何使用 XmlTextReader 類(lèi)讀一個(gè) XML 文檔并在控制臺(tái)顯示。引入命名空間 - 由于 Xml 類(lèi)被定義在 System.XML 命名空間中,做的第一件事是需要將 System.X

58、ML 命名空間引入項(xiàng)目。打開(kāi) XML 文檔 - XmlTextReader 類(lèi)的構(gòu)造函數(shù)打開(kāi)一個(gè) XML 文檔。在示例中,使用“C:temp”下的名為“xmltest.xml”的 XML 文件。可以使用需要的文件。讀數(shù)據(jù) - XmlTextReader 的 Read 方法數(shù)據(jù)。示例代碼26namespaceReadXMLusingSystem;usingSystem.Xml;publicclassClass1publicClass1() public sicMain(stringargs) try /OpenanXMLfileXmlTextReaderreader =newXmlTextRea

59、der(C:tempxmltest.xml);while ( reader.Read() ) Console.Wriine(reader.Name);while(reader.Read()Console.Wriine(reader.Name);/ Open an XML fileXmlTextReader reader = new XmlTextReader(C:tempxmltest.xml);using System.Xml; catch(Exceptione)(Exception:Console.Wriine0,e.ToString();return0;參考地址:/index.?topic=898.0用 XmlWriter 寫(xiě) XML 文件XmlWriter 和 XmlTextWriter 類(lèi)定義在“System.XML”命名空間中。 XmlTextWriter 類(lèi)繼承自 XmlWriter 類(lèi), 表示一個(gè)提供快速非緩存的生成基于 W3C 可擴(kuò)展標(biāo)記語(yǔ)言(XML)1.0 版本的寫(xiě)器。在這篇文章中將顯示如何使用 XmlTextWriter 類(lèi)創(chuàng)建 XML 文檔并將信息寫(xiě)入文檔。引入命名空間 - 由于 Xml 類(lèi)被定義在 System.XML 命名空間中,做的第一件事是需要將 System.XML 命名空間引入項(xiàng)目。創(chuàng)建 XML 文檔 - 如果文件不存在 XmlTextWrit

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論