تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
انشاء و الكتابه في ملف جديد c#
#1
كاتب الموضوع : AhmedEssawy


كود :
[align=left][FONT=Courier New][color=green]// This subrouting uses a StreamWriter object to create a new file[/color][/FONT]
[FONT=Courier New][color=green]// and fill it with the text in txtFileText. It first checks to see[/color][/FONT]
[FONT=Courier New][color=green]// if the file already exists and prompts to overwrite it.[/color][/FONT]
[FONT=Courier New][color=blue]private[/color] [color=blue]void[/color] btnStreamWriterCreateFile_Click([color=blue]object[/color] sender, System.[color=teal]EventArgs[/color] e)[/FONT]
[FONT=Courier New]{[/FONT]
[FONT=Courier New][color=green]// Check to see if the user is writing over an existing file.[/color][/FONT]
[FONT=Courier New][color=blue]if[/color] ([color=teal]File[/color].Exists(txtFileName.Text))[/FONT]
[FONT=Courier New]{[/FONT]
[FONT=Courier New][color=blue]if[/color]([color=teal]MessageBox[/color].Show([color=maroon]"That file exists. Would you like to overwrite it?"[/color], [color=maroon]"Confirm"[/color], [color=teal]MessageBoxButtons[/color].YesNo) != [color=teal]DialogResult[/color].Yes)[/FONT]
[FONT=Courier New]{[/FONT]
[FONT=Courier New][color=green]// exit method[/color][/FONT]
[FONT=Courier New][color=blue]return[/color];[/FONT]
[FONT=Courier New]}[/FONT]
[FONT=Courier New]}[/FONT]

[FONT=Courier New][color=green]// The StreamWriter must be defined outside of the try-catch block[/color][/FONT]
[FONT=Courier New][color=green]// in order to reference it in the Finally block.[/color][/FONT]
[FONT=Courier New][color=teal]StreamWriter[/color] myStreamWriter = [color=blue]null[/color];[/FONT]

[FONT=Courier New][color=green]// Ensure that the creation of the new StreamWriter is wrapped in a [/color][/FONT]
[FONT=Courier New][color=green]// try-catch block, since an invalid filename could have been used.[/color][/FONT]
[FONT=Courier New][color=blue]try[/color] [/FONT]
[FONT=Courier New]{[/FONT]
[FONT=Courier New][color=green]// Create a StreamWriter using a static File class.[/color][/FONT]
[FONT=Courier New]myStreamWriter = [color=teal]File[/color].CreateText(txtFileName.Text);[/FONT]

[FONT=Courier New][color=green]// Write the entire contents of the txtFileText text box[/color][/FONT]
[FONT=Courier New][color=green]// to the StreamWriter in one shot.[/color][/FONT]
[FONT=Courier New]myStreamWriter.Write(txtFileText.Text);[/FONT]
[FONT=Courier New]myStreamWriter.Flush();[/FONT]
[FONT=Courier New]} [/FONT]
[FONT=Courier New][color=blue]catch[/color]([color=teal]Exception[/color] exc)[/FONT]
[FONT=Courier New]{[/FONT]
[FONT=Courier New][color=green]// Show the error to the user.[/color][/FONT]
[FONT=Courier New][color=teal]MessageBox[/color].Show([color=maroon]"File could not be created or written to."[/color] + [color=teal]Environment[/color].NewLine + [color=maroon]"Please verify that the filename is correct, and that you have write permissions for the desired directory."[/color] + [color=teal]Environment[/color].NewLine + [color=teal]Environment[/color].NewLine + [color=maroon]"Exception: "[/color] + exc.Message);[/FONT]
[FONT=Courier New]}[/FONT]
[FONT=Courier New][color=blue]finally[/color][/FONT]
[FONT=Courier New]{[/FONT]
[FONT=Courier New][color=green]// Close the object if it has been created.[/color][/FONT]
[FONT=Courier New][color=blue]if[/color] (myStreamWriter != [color=blue]null[/color])[/FONT]
[FONT=Courier New]{[/FONT]
[FONT=Courier New]myStreamWriter.Close();[/FONT]
[FONT=Courier New]}[/FONT]
[FONT=Courier New]} }[/FONT][/align]
}}}}
تم الشكر بواسطة:


المواضيع المحتمل أن تكون متشابهة .
الموضوع : الكاتب الردود : المشاهدات : آخر رد
  كيف تكون مجلد جديد RaggiTech 0 396 17-10-12, 09:22 PM
آخر رد: RaggiTech
  نسخ .. انشاء ... الغاء ...اعاده تسميه مجلد folderمعين فيجوال بيسك RaggiTech 0 928 17-10-12, 05:45 PM
آخر رد: RaggiTech
  انشاء و الغاء مفتاح ريجستري RaggiTech 0 311 17-10-12, 05:03 PM
آخر رد: RaggiTech

التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم