当前位置:网站首页>C disk drives, folders and file operations
C disk drives, folders and file operations
2022-06-25 08:00:00 【ykxxcs】
c# Operation of files and folders
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Driveinfo driveInfo = new Driveinfo("C"); // Copied it from the Internet , There is a mistake
DriveInfo driveInfo = new DriveInfo("C");
MessageBox.Show(driveInfo.ToString());
MessageBox.Show(" Name of the drive :" + driveInfo.Name);
MessageBox.Show(" The root directory of the drive :" + driveInfo.RootDirectory);
MessageBox.Show(" Whether the drive is ready :" + driveInfo.IsReady);
MessageBox.Show(" Amount of free space available on disk :" + driveInfo.AvailableFreeSpace);
MessageBox.Show(" Total free space available on drive :" + driveInfo.TotalFreeSpace);
MessageBox.Show(" Total storage space on drive :" + driveInfo.TotalSize);
MessageBox.Show(" File system format name :" + driveInfo.DriveFormat);
MessageBox.Show(" Drive type :" + driveInfo.DriveType);
MessageBox.Show(" The volume label of the drive :" + driveInfo.VolumeLabel);
}
// Get all drive names and file formats in your computer
private void button2_Click(object sender, EventArgs e)
{
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
if (drive.IsReady)
{
MessageBox.Show(" Drive name :" + drive.Name);
MessageBox.Show(" File format :" + drive.DriveFormat);
}
}
}
// Use Directory Class in D On disk operation code Folder , It is required to judge whether the folder exists first ,
private void button3_Click(object sender, EventArgs e)
{
// Delete... If it exists , Otherwise, create the folder
if (Directory.Exists(@"D:\code"))
Directory.Delete(@"D:\code", true);
else
Directory.CreateDirectory(@"D:\code");
MessageBox.Show("ok");
}
private void button4_Click(object sender, EventArgs e)
{
// stay D Discoid code Create a folder named test1.txt The file of , And get the relevant attributes of the file , Then move it to D On the plate code-1 In the folder .
Directory.CreateDirectory(@"D:\code");
FileInfo fileInfo = new FileInfo(@"D:\code\test1.txt");
if (!fileInfo.Exists)
fileInfo.Create().Close();// create a file .Close() Close the stream and release all resources associated with it
fileInfo.Attributes = FileAttributes.Normal;
MessageBox.Show(" file name :" + fileInfo.Name);
MessageBox.Show(" File parent directory :" + fileInfo.Directory);
MessageBox.Show(" Full directory of files :" + fileInfo.FullName);
MessageBox.Show(" Full path to directory :" + fileInfo.DirectoryName);
MessageBox.Show(" File creation time :" + fileInfo.CreationTime);
MessageBox.Show(" File extension :" + fileInfo.Extension);
MessageBox.Show(" Whether the file is read-only :" + fileInfo.IsReadOnly);
MessageBox.Show(" Last accessed file time :" + fileInfo.LastAccessTime);
MessageBox.Show(" Last write file time :" + fileInfo.LastWriteTime);
MessageBox.Show(" file size :" + fileInfo.Length);
Directory.CreateDirectory(@"D:\code-1");// establish code-1 Folder
FileInfo newFileInfo = new FileInfo(@"D:\code-1\test1.txt");// Judge code-1 Whether there is... Under the folder test1.txt
if (!newFileInfo.Exists)
fileInfo.MoveTo(@"D:\code-1\test1.txt");// If it doesn't exist, move the file
}
private void button5_Click(object sender, EventArgs e)
{
// Path path = new Path();
//string path = @"d:\code";
}
private void button6_Click(object sender, EventArgs e)
{
// Directory.CreateDirectory(@"D:\code");// Create directory
// File.Create(@"D:\code\test.txt").Close(); // Create files in the directory
//==================================== Read D disc code Under the folder test.txt Information in the file . ============
string path = @"D:\code\test.txt";
StreamReader reader = new StreamReader(path);
if (reader.Peek() != -1)// Determine whether there are characters in the file
{
// string str = reader.ReadLine();// Read only one line
string str = reader.ReadToEnd();// Read it all
MessageBox.Show(str);
Console.ReadLine();
}
reader.Close();
}
private void button7_Click(object sender, EventArgs e)
{
// towards D disc code The folder test.txt Write your name and mobile number in the file
string path = @"D:\code\test.txt";
using (StreamWriter writer = new StreamWriter(path))
{
writer.WriteLine(" full name ");
writer.WriteLine(" Phone number ");
writer.Flush();// Refresh cache
}
}
private void button8_Click(object sender, EventArgs e)
{
// stay D disc code The folder student.txt Write the student number information in the file
File.Create(@"D:\code\student.txt").Close();
string path = @"D:\code\student.txt";
string mes = " Student number :3188906224";
byte[] bytes = Encoding.UTF8.GetBytes(mes);// Convert data from string type to byte type
using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Write))
{
fileStream.Write(bytes, 0, bytes.Length);
fileStream.Flush();
}
// from D Discoid code In the folder student.txt The student number in the file is read out , And display it on the console
if (File.Exists(path))
{
using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
byte[] b = new byte[fileStream.Length];// Define the byte array for storing file information
fileStream.Read(b, 0, b.Length);// Read file information
char[] c = Encoding.UTF8.GetChars(b);// Convert the read data from byte type to character type
Console.WriteLine(c);
}
}
else
Console.WriteLine(" file does not exist ");
Console.ReadLine();
}
}
}
Here it is , Thank you, blogger : Pistachio 2021, Part of this article is transferred from the following link
https://blog.csdn.net/weixin_56814032/article/details/120242871
边栏推荐
- 不怕百战失利,就怕灰心丧气
- 电子学:第008课——实验 6:非常简单的开关
- MySQL interview - the response of executing SQL is relatively slow, and the troubleshooting ideas.
- 电子学:第009课——实验 7:研究继电器
- 电子学:第010课——实验 9:时间与电容器
- Dietary intervention reduces cancer treatment-related symptoms and toxicity
- MySQL简单权限管理
- Can transparent cloud gateway caniot and candtu record can messages and send and receive can data remotely
- Importer des données dans MATLAB
- Modular programming of LCD1602 LCD controlled by single chip microcomputer
猜你喜欢
allgero报错:Program has encountered a problem and must exit. The design will be saved as a .SAV file
唐老师讲运算放大器(第七讲)——运放的应用
共话云原生数据库的未来
Authority design of SaaS system based on RBAC
How to select lead-free and lead-free tin spraying for PCB? 2021-11-16
[little knowledge] PCB proofing process
电子学:第012课——实验 13:烧烤 LED
传统的IO存在什么问题?为什么引入零拷贝的?
Dietary intervention reduces cancer treatment-related symptoms and toxicity
饮食干预减轻癌症治疗相关症状和毒性
随机推荐
50. pow (x, n) - fast power
Pycharm的奇葩设定:取消注释后立马复制会带上#
This article uses pytorch to build Gan model!
【论文学习】《VQMIVC》
How to create a new branch with SVN
MySQL simple permission management
2021ICPC网络赛第一场
线程+线程问题记录
【红旗杯?】补题
电子学:第009课——实验 7:研究继电器
50 pieces of professional knowledge of Product Manager (IV) - from problem to ability improvement: amdgf model tool
27. 移除元素
How to resize an image in C #
Debugging mipi-dsi screen based on stm32mp157
【日常训练】207. 课程表
网络模型——OSI模型与TCP/IP模型
飞机引气系统的建模与故障仿真
TCP与UDP
1464. 数组中两元素的最大乘积
【补题】2021牛客暑期多校训练营4-n