0% found this document useful (0 votes)
4 views7 pages

C# File Operations: Create, Read, Delete

Uploaded by

anh70783
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

C# File Operations: Create, Read, Delete

Uploaded by

anh70783
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

using System;

using [Link];

using [Link];

class Program

static void Main()

string path = "[Link]";

[Link]("Chọn bài (1-15): ");

string input = [Link]();

if (![Link](input, out int choice))

[Link]("❌Lỗi: bạn chưa nhập số hợp lệ!");

return;

switch (choice)

case 1: // create blank file

[Link](path).Close();

[Link]("File created.");

break;
case 2: // remove file

if ([Link](path))

[Link](path);

[Link]("File deleted.");

else [Link]("File không tồn tại.");

break;

case 3: // create file and add text

[Link](path, "Hello world!");

[Link]("Text ne moi nguoi oi");

break;

case 4: // read text file

if ([Link](path))

[Link]([Link](path));

else [Link]("File không tồn tại.");

break;

case 5: // write array of strings

string[] lines = { "One", "Two", "Three" };

[Link](path, lines);

[Link]("Array được ghi ra nè");

break;

case 6: // append text


if ([Link](path))

[Link](path, "\nAppended line");

else

[Link](path, "Appended line");

[Link]("Text appended.");

break;

case 7: // copy file

if ([Link](path))

[Link](path, "[Link]", true);

[Link]("File is copied.");

else [Link]("File không tồn tại.");

break;

case 8: // move/rename file

string newPath = "[Link]";

if ([Link](path))

if ([Link](newPath)) [Link](newPath); // tránh lỗi


overwrite

[Link](path, newPath);

[Link]("File is moved.");

else [Link]("File không tồn tại.");

break;
case 9: // read first line

if ([Link]("[Link]"))

[Link]([Link]("[Link]").First());

else [Link]("File [Link] không tồn tại.");

break;

case 10: // read last line

if ([Link]("[Link]"))

[Link]([Link]("[Link]").Last());

else [Link]("File [Link] không tồn tại.");

break;

case 11: // read last n lines

if ([Link]("[Link]"))

[Link]("Nhập n: ");

string nInput = [Link]();

if ([Link](nInput, out int n))

var lastN =
[Link]("[Link]").Reverse().Take(n).Reverse();

foreach (var line in lastN) [Link](line);


}

else [Link]("n không hợp lệ.");

else [Link]("File [Link] không tồn tại.");

break;

case 12: // read specific line

if ([Link]("[Link]"))

[Link]("Nhập số dòng: ");

string Input1 = [Link]();

if ([Link](Input1, out int lineso))

var line = [Link]("[Link]").Skip(lineso -


1).FirstOrDefault();

if (line == null) [Link]("Dòng không tồn tại.");

else [Link](line);

else [Link]("Số dòng không hợp lệ.");

else [Link]("File [Link] không tồn tại.");

break;

case 13: // count lines

if ([Link]("[Link]"))

[Link]("Số dòng: " +


[Link]("[Link]").Length);
else [Link]("File [Link] không tồn tại.");

break;

case 14: // print folder structure

string folder = [Link]();

[Link]("Files in " + folder);

foreach (var f in [Link](folder))

[Link](f);

foreach (var d in [Link](folder))

[Link](d);

break;

case 15: // count characters

if ([Link]("[Link]"))

string text = [Link]("[Link]");

var stats = [Link](c => c)

.Select(g => new { Char = [Link], Count =


[Link]() });

foreach (var s in stats)

[Link]($"{[Link]}: {[Link]}");

else [Link]("File [Link] không tồn tại.");

break;

default:

[Link]("❌ Lựa chọn không hợp lệ.");


break;

You might also like