高级程序设计实验报告

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

高级程序设计

实验报告

班级

学号:

姓名:

实验名称:

指导老师:

日期:

实验十一文件和流I/O

一、实验题目:文件和流I/O

二、实验内容:

1.掌握磁盘的基本操作

2.掌握目录的基本操作

3.文件的基本操作

4.文本文件的读取和写入

5.字符串的读取和写入

6.二进制文件的读取和写入I

7. 二进制文件的读取和写入2

三、主要程序代码:

1. using System;

using System.IO;

using System.Collections.Generic;

using System.Text;

namespace实验1

{

class Test

{

public static void Main()

{

DriveInfo[] allDrives = DriveInfo.GetDrives();

foreach (DriveInfo d in allDrives)

{

Console.WriteLine("Drive {0}", );

Console.WriteLine(" File type: {0}", d.DriveType);

if (d.IsReady == true)

{

Console.WriteLine(" Volume label: {0}", d.VolumeLabel);

Console.WriteLine(" File system: {0}", d.DriveFormat);

Console.WriteLine(" Available space to current user:{0, 15}

bytes",d.AvailableFreeSpace);

Console.WriteLine("Total available space: {0, 15} bytes",d.TotalFreeSpace);

Console.WriteLine( " Total size of drive: {0, 15} bytes ", d.TotalSize); }

}

Console.ReadKey();

}

}

2. using System;

using System.IO;

namespace实验11

{

class Program

{

static void Main(string[] args)

{

String path0 = @"C:\WINDOWS\security";

String path1 = @"C:\WINDOWS";

try{

Console.WriteLine("应用程序当前工作目录为:\n

{0}",Directory.GetCurrentDirectory());

string [] subdirectotyEntries

=Directory.GetDirectories(Directory.GetDirectoryRoot(Directory.GetCurrentDirectory()));

Console.WriteLine("根目录中子目录总数为{0},子目录名称分别

为:",subdirectotyEntries.Length);

foreach (string subdirectory in subdirectotyEntries)

Console.WriteLine(" {0}", subdirectory);

string[] dirs =Directory.GetFiles(path0);

Console.WriteLine("目录{0}下的总文件总数为 {1}。文件名称分别为:", path0, dirs.Length);

foreach (string dir in dirs) Console.WriteLine(" {0}", dir);

string[] dirs1 = Directory.GetFiles(path1, " *.ini");

Console.WriteLine("目录{0}下拓展名为.sys的文件总数为 {1}。文件名称分别为:

",path1, dirs1.Length);

foreach (string dir in dirs1) Console.WriteLine(" {0}", dir);

}

catch (Exception err)

{ Console.WriteLine("\n操作失败: {0}", err.Message); }

Console.ReadLine();

}

}

}

3 using System;

using System.IO;

using System.Collections.Generic;

using System.Text;

namespace文件的基本操作

{

class Program

{

static void Main(string[] args)

{

string s;

string path = @"c:\temp\SrcFile.txt";

FileInfo fi1 = new FileInfo(path);

if (!fi1.Exists)

{

using (StreamWriter sw = fi1.CreateText())

{

bool bContinue = true;

while (bContinue)

{

Console.Write("请输入文件内容:");

s = Console.ReadLine();

if (s != "") sw.WriteLine(s);

else bContinue = false;

}

}

}

fi1 = new FileInfo(path);

Console.WriteLine("源文件{0}所在的目录为:\n\t{1}", path, fi1.DirectoryName);

Console.WriteLine("源文件{0}长度为:\n\t{1}", path, fi1.Length);

Console.WriteLine("源文件{0}内容为:", path);

using (StreamReader sr = fi1.OpenText())

{

s = "";

while ((s = sr.ReadLine()) != null) Console.WriteLine("\t{0}", s);

}

try

{

string path2 = @"c:\temp\DesFile.txt";

FileInfo fi2 = new FileInfo(path2);

fi2.Delete();

fi1.CopyTo(path2);

Console.WriteLine("源文件成功复制至目标文件:\n\t{0}", path2);

fi2.Delete();

Console.WriteLine("目标文件{0}成功删除", path2);

}

相关文档
最新文档