منتدى قسم تكنولوجيا المعلومات في مدرسة الدوحة الثانوية المستقلة للبنين
علمت أن رزقي لن يأخذه غيري فاطمأن قلبي

قم وذق طعم الصلاة في دجى الليل الطويل

قم وجاهد في الحياة ان مثوانا قليل
منتدى قسم تكنولوجيا المعلومات في مدرسة الدوحة الثانوية المستقلة للبنين
علمت أن رزقي لن يأخذه غيري فاطمأن قلبي

قم وذق طعم الصلاة في دجى الليل الطويل

قم وجاهد في الحياة ان مثوانا قليل
منتدى قسم تكنولوجيا المعلومات في مدرسة الدوحة الثانوية المستقلة للبنين
هل تريد التفاعل مع هذه المساهمة؟ كل ما عليك هو إنشاء حساب جديد ببضع خطوات أو تسجيل الدخول للمتابعة.

منتدى قسم تكنولوجيا المعلومات في مدرسة الدوحة الثانوية المستقلة للبنين

من أفضل مدارس قطر
 
الرئيسيةأحدث الصورالتسجيلدخول
قال الامام احمد بن حنبل: إن لنا إخوان لانراهم إلا مره كل سنه , نحن اوثق بمودتهم ممن نراهم كل يوم .أسعد الله قلوبا طاهره إن وصلناها شكرت وإن قصرنا عذرت
من العظماء من يشعر المرء فى حضرته أنه صغير ولكن العظيم بحق هو من يشعر الجميع في حضرته بأنهم عظماء
كم في المقابر من يحسدونك على هذه الأيام والليالي التي تعيشها يتمنون لو تسبيحة أو استغفار ينفعهم عند ربهم أو سجدة تنير قبورهم أو صدقة تظلهم بين يدي الملك الجبار .. فقط تذكر .. ولا تضيع الفرصة التي بين يديك

 

 Input, Output, Other File Operations, and Persisting Data

اذهب الى الأسفل 
3 مشترك
كاتب الموضوعرسالة
A.Tamimi
Admin
A.Tamimi


Posts : 1593
أهمية العضو : 16
Join date : 13/11/2008
Age : 39
Location : Jordan

Input, Output, Other File Operations, and Persisting Data Empty
مُساهمةموضوع: Input, Output, Other File Operations, and Persisting Data   Input, Output, Other File Operations, and Persisting Data Emptyالأربعاء ديسمبر 10, 2008 3:54 am

Creating Files Programmatically
The first thing you need to know about file operations in C#, such as writing to a file, reading data from a file, etc., is that you must place a using reference to System.IO at the top of your source code (just beneath using System; is a good place):
Using System;
Using System.IO;


As you can see in line #2, we include a reference to the System.IO namespace, which contains classes for working with input and output.

We can add a single line of code (line #Cool inside our Main() method, to create a text file in our application's directory:
Input, Output, Other File Operations, and Persisting Data Srccod10

Run the program, then navigate to the application's executable directory under your projects folder, to confirm that "myfile.txt" has indeed been created:
Input, Output, Other File Operations, and Persisting Data Confir10

Notice that the file shows 0 Kb, which is what we would expect. We merely created a text file. We did not write any data to it. The file can have any extension you want, not just .txt, or no extension at all, but consider that here we created a text file (see line #8 in the source code) and therefore the file is written using ASCII characters. A binary file is created the same way, just that instead of using File.CreateText() you use File.Create(). At first you won't notice any difference between the text file and the binary file but it matters a lot because it is stored differently. You use the text file for storing text and the binary file for storing non-text data.

Let's run the program again, but before we do so, let's update the source code so that it will actually cause the created text file to contain a line of text. We'll use something called a StreamWriter object. This object is instantiated from a class in the System.IO namespace that is used for writing data to files.
Input, Output, Other File Operations, and Persisting Data Stream10


Now, run the program, then navigate to the application directory and examine the "myfile.txt" file. You'll see that it is no longer an empty file. It should now show 1 Kb:

If you double-click the file and examine its contents, you'll see something like the following:
Input, Output, Other File Operations, and Persisting Data Life_w10

It should be apparent what is going on here. With the help of a StreamWriter object, we are writing a string to the text file. You could replace the string I used, in line # 10, with any string of your choice, such as "E pluribus unum" or "Mi casa su casa". Use the program we just wrote to write the following line of text to the file: The name is Bond... James Bond. Then exit the program and prepare to write a different program.


Reading a Text File
We can use C# to read either text or binary files. For the purpose of this introductory level tutorial, we'll only be focusing on text files. In a later tutorial, in another series altogether, I'll address writing and reading binary files.

What we're going to do now is write a program that will open the "myfile.txt" text file and read the line of text that it contains. Delete the current code in the project source code window, and paste this source code. Run this new program, and you should see the following output:
Input, Output, Other File Operations, and Persisting Data Stream11

It is also possible to read text from a file one character at a time. Sometimes this can be useful, although in our previous example, the ReadLine() method of the StreamReader class was more appropriate. However, to see how to do it by reading it one character at a time, examine the following source code listing:
Input, Output, Other File Operations, and Persisting Data Read_e10

Note that we are using an integer variable to hold the value returned by each call to sr.Read(); this is due to the fact that the method returns an integer value that is the ASCII value of the character just read from the file. We then have to convert that ASCII value to a character (as you can see in line # 16). Probably the most complex line of code in the source code listing shown above is line # 14. If the StreamReader object attempts to read in a character from the file and is unsuccessful due to the end of the file having been reached, a value of negative one is returned.



Checking For File Existence
If you attempt to open a file that isn't there, an exception will be thrown. Exceptions are ugly. They make users feel that your software is buggy (not without some justification). You should check to ensure that a particular file exists on disk before attempting to open it. The System.IO namespace provides the FileExists() method, which will return true if the file path you pass into the method as a parameter is valid, or false if it isn't. If your program creates a file and does not specify a path for it, it will, by default, be created in the directory in which the executable that is attempting to create the file is located. In C# projects, this will be the Debug or Release directory of the particular project you're working on. I used this default directory in my examples above, where I demonstrated writing to a file, and then reading from that file.
Input, Output, Other File Operations, and Persisting Data Curren10


You have to be careful about making assumptions when programming. You should not automatically assume that you're current directory is the application executable's directory, because code can change the working directory. To demonstrate this, I've added a single line of code (line #8 in the image below) to one of the example programs we did earlier. The only thing line #8 does, below, is set the current directory to somewhere other than the application directory (which is where our file was created). You can see the nasty error that is caused when you assume that you're in the correct directory when attempting to open a file.

You may not clearly understand the rather unusual looking code in the example program for which I provide a link above. For example,

if ((fi.Attributes & FileAttributes.ReadOnly) != 0){
// do this...
}

To break that down for you, simply know that whenever you see something in C# that looks like this:


if((BlahBlahThis & BlahBlahThat) != 0)

...what's being said is "if it's true that the stuff inside the nested parentheses is the case, then do the following..." You can think of !=0 as "not not the case", in other words is the case.

There are other attributes that we didn't use in the code but you can easily figure them out thanks to IntelliSense, if you're using the Visual C# 2005 or the Visual C# 2005 Express edition IDE.


Moving a File to Another Location
There is a method of the File object named Copy(). You can use it to copy a file from one location to another. This source code listing shows an example of copying a file from the application directory to your desktop directory. Copying a file doesn't eliminate the original file. If you want to move a file, i.e., make it disappear from its current location and reappear in another directory, then you first copy it to the desired destination, then follow up by deleting it from the source directory. So, in the above example, you could add the following line immediately after the invocation of the File object's Copy() method, to change the program's function from copying to moving the file:

File.Delete("myfile.txt"); //deletes original file from the application's executable directory


Input, Output, Other File Operations, and Persisting Data Thumb110
الرجوع الى أعلى الصفحة اذهب الى الأسفل
https://falcons.aforumfree.com
????
زائر




Input, Output, Other File Operations, and Persisting Data Empty
مُساهمةموضوع: رد: Input, Output, Other File Operations, and Persisting Data   Input, Output, Other File Operations, and Persisting Data Emptyالثلاثاء سبتمبر 15, 2009 8:46 pm

شو هذا مش فاهم اشي الو علاقة بالحاسوب ممن تفهمني اياه او تعطيني فكرة عنو
الرجوع الى أعلى الصفحة اذهب الى الأسفل
????
زائر




Input, Output, Other File Operations, and Persisting Data Empty
مُساهمةموضوع: رد: Input, Output, Other File Operations, and Persisting Data   Input, Output, Other File Operations, and Persisting Data Emptyالثلاثاء سبتمبر 15, 2009 8:47 pm

يا باطل
الرجوع الى أعلى الصفحة اذهب الى الأسفل
يوسف الشلعوط

يوسف الشلعوط


Posts : 601
أهمية العضو : 3
Join date : 13/09/2009
Age : 30
Location : عمان

Input, Output, Other File Operations, and Persisting Data Empty
مُساهمةموضوع: رد: Input, Output, Other File Operations, and Persisting Data   Input, Output, Other File Operations, and Persisting Data Emptyالأربعاء سبتمبر 16, 2009 3:25 am

ههههههههه
الرجوع الى أعلى الصفحة اذهب الى الأسفل
A.Tamimi
Admin
A.Tamimi


Posts : 1593
أهمية العضو : 16
Join date : 13/11/2008
Age : 39
Location : Jordan

Input, Output, Other File Operations, and Persisting Data Empty
مُساهمةموضوع: ممممم..   Input, Output, Other File Operations, and Persisting Data Emptyالأربعاء سبتمبر 16, 2009 7:17 am


طريقة انشاء ملف نصي

باستخدام لغة السي شارب

طبعا هاي لغة برمجة
الرجوع الى أعلى الصفحة اذهب الى الأسفل
https://falcons.aforumfree.com
????
زائر




Input, Output, Other File Operations, and Persisting Data Empty
مُساهمةموضوع: رد: Input, Output, Other File Operations, and Persisting Data   Input, Output, Other File Operations, and Persisting Data Emptyالأربعاء سبتمبر 16, 2009 10:40 pm

يا فهمان ما انا بعرف ههههههههههه
الرجوع الى أعلى الصفحة اذهب الى الأسفل
يوسف الشلعوط

يوسف الشلعوط


Posts : 601
أهمية العضو : 3
Join date : 13/09/2009
Age : 30
Location : عمان

Input, Output, Other File Operations, and Persisting Data Empty
مُساهمةموضوع: رد: Input, Output, Other File Operations, and Persisting Data   Input, Output, Other File Operations, and Persisting Data Emptyالخميس سبتمبر 17, 2009 6:55 pm

هههههههههههه
الرجوع الى أعلى الصفحة اذهب الى الأسفل
M.ALS3OD

M.ALS3OD


Posts : 854
أهمية العضو : 0
Join date : 13/09/2009
Age : 30
Location : AMMAN

Input, Output, Other File Operations, and Persisting Data Empty
مُساهمةموضوع: رد: Input, Output, Other File Operations, and Persisting Data   Input, Output, Other File Operations, and Persisting Data Emptyالسبت سبتمبر 19, 2009 1:29 am

ســـهـــل بس انا مش حاب افهمه مـــشـــكـــوررررررررررر
الرجوع الى أعلى الصفحة اذهب الى الأسفل
 
Input, Output, Other File Operations, and Persisting Data
الرجوع الى أعلى الصفحة 
صفحة 1 من اصل 1
 مواضيع مماثلة
-
» Data Models, Schemas and Instances
» Data Modeling Using Entity-Relationship Model
» rename a file in C#
» Update an XML file using a DataSet

صلاحيات هذا المنتدى:لاتستطيع الرد على المواضيع في هذا المنتدى
منتدى قسم تكنولوجيا المعلومات في مدرسة الدوحة الثانوية المستقلة للبنين :: ----§§§§ المنتديات التقنية والبرمجية §§§§---- :: قسم الدوت نت لتطوير الويب والديسكتوب-
انتقل الى: