使用File、Path和Directory进行常见的操作

我们偶尔会用到文件操作,其中File、Path和Directory这三个类是比较常见的,今天写了一个测试demo,也是顺便学习一下,记录一二。

BTW,使用这几个类的时候需要引用using System.IO命名空间。

 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             //ProcessPath();
 6             //ProcessFile();
 7             //ReadFileByByteArray();
 8             //ReadFileByAllLines();
 9             //ReadFileByAllText();
10             //WriteFileByByteArray();
11             //WriteFileByLines();
12             //WriteFileByText();
13             UseDirectory();
14             Console.ReadKey();
15         }
16         /// <summary>
17         /// path类只是在string层面进行操作
18         /// </summary>
19         private static void ProcessPath()
20         {
21             string path = @"C:\Users\Administrator\Desktop\XX-Net.txt";
22             string directory = Path.GetDirectoryName(path);
23             string fileName = Path.GetFileName(path);
24             string fileNamenoExtension = Path.GetFileNameWithoutExtension(path);
25             string fileExtension = Path.GetExtension(path);
26             string ss = Path.ChangeExtension(path, "csv");
27             Console.WriteLine($"directory is {directory},fileName is {fileName},file without extension is{fileNamenoExtension},extension is {fileExtension},new file is{ss} ");
28         }
29         private static void ProcessFile()
30         {
31             string path = @"C:\Users\Administrator\Desktop\2.txt";
32             //File.Create(path);
33             //File.Delete(path);
34             //File.Copy(path, @"C:\Users\Administrator\Desktop\3.txt");
35             File.Move(path, @"C:\Users\Administrator\Desktop\4.txt");
36             Console.WriteLine("creat successfully...");
37         }
38         private static void ReadFileByByteArray()
39         {
40             string path = @"C:\Users\Administrator\Desktop\3.txt";
41             byte[] fileByte = File.ReadAllBytes(path);
42             string fileByByte = Encoding.Default.GetString(fileByte);
43             Console.WriteLine(fileByByte);
44         }
45         private static void ReadFileByAllLines()
46         {
47             string path = @"C:\Users\Administrator\Desktop\3.txt";
48             string[] fileLines = File.ReadAllLines(path,Encoding.Default);
49             foreach (var line in fileLines)
50             {
51                 Console.WriteLine(line);
52             }
53         }
54         private static void ReadFileByAllText()
55         {
56             string path = @"C:\Users\Administrator\Desktop\3.txt";
57             string fileText = File.ReadAllText(path,Encoding.Default);
58             Console.WriteLine(fileText);
59         }
60         private static void WriteFileByByteArray()
61         {
62             string path = @"C:\Users\Administrator\Desktop\1.txt";
63             string fileStr = "大家好,我来了,我的号码是1388888888";
64             byte[] fileByte = Encoding.Default.GetBytes(fileStr);
65             File.WriteAllBytes(path, fileByte);
66             Console.WriteLine("Write Sucessfully...");
67         }
68         private static void WriteFileByLines()
69         {
70             string path = @"C:\Users\Administrator\Desktop\1.txt";
71             string[] fileArray = { "你好","我的名字","叫Tom","我来自安徽省阜阳市太和县","他们","都知道我是个好人" };
72             File.WriteAllLines(path, fileArray);
73             Console.WriteLine("Write Sucessfully...");
74         }
75         private static void WriteFileByText()
76         {
77             string path = @"C:\Users\Administrator\Desktop\1.txt";
78             //File.WriteAllText(path, "落霞与孤鹜齐飞,秋水共长天一色。");
79             File.AppendAllText(path, "宜将剩勇追穷寇,不可沽名学霸王!");
80             Console.WriteLine("Write Sucessfully...");
81         }
82         private static void UseDirectory()
83         {
84             //Directory.CreateDirectory(@"C:\Users\Administrator\Desktop\NewFolder");
85             //Directory.Delete(@"C:\Users\Administrator\Desktop\NewFolder",true);
86
87             string[] files = Directory.GetFiles(@"D:\迅雷下载", "*.rmvb",SearchOption.TopDirectoryOnly);
88             foreach (var file in files)
89             {
90                 Console.WriteLine(file);
91             }
92             Console.WriteLine("Get File Successfully...");
93
94         }
95     }
 

时间: 2024-10-11 00:59:45

使用File、Path和Directory进行常见的操作的相关文章

388. Longest Absolute File Path

题目如下: Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents: dir subdir1 subdir2 file.ext The directory dir contains an empty sub-directory subdir1 and a sub-dire

[LintCode] System Longest File Path

Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents: dir subdir1 subdir2 file.ext The directory dir contains an empty sub-directory subdir1 and a sub-directory 

Longest Absolute File Path -- LeetCode

Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents: dir subdir1 subdir2 file.ext The directory dir contains an empty sub-directory subdir1 and a sub-directory 

Leetcode: Longest Absolute File Path

Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents: dir subdir1 subdir2 file.ext The directory dir contains an empty sub-directory subdir1 and a sub-directory

算法比赛(1)----Longest Absolute File Path

Leetcode算法比赛题目之:Longest Absolute File Path 原文链接:https://leetcode.com/contest/1/problems/longest-absolute-file-path/ Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" rep

LeetCode 388. Longest Absolute File Path

原题链接在这里:https://leetcode.com/problems/longest-absolute-file-path/ 题目: Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents: dir subdir1 subdir2 file.ext The dire

Error in read.dcf(file.path(pkgname, &quot;DESCRIPTION&quot;), c(&quot;Package&quot;, &quot;Type&quot;))报错解决方式

安装R包时产生如下错误: Error in read.dcf(file.path(pkgname, "DESCRIPTION"), c("Package", "Type")) : 无法打开链结 此外: Warning messages: 1: In download.file(url, destfile, method, mode = "wb", ...) : downloaded length 409600 != repor

安卓各文件存储路径汇总(Android file path)

写下来,省得以后不记得到处翻: Environment.getDataDirectory() = /data Environment.getDownloadCacheDirectory() = /cache Environment.getExternalStorageDirectory() = /mnt/sdcard Environment.getExternalStoragePublicDirectory(“test”) = /mnt/sdcard/test Environment.getRo

《ArcGIS Runtime SDK for Android开发笔记》——问题集:Error:Error: File path too long on Windows, keep below 240 characters

1.前言 在使用Android Studio开发环境时,经常会爆出以下错误,虽然具体细节内容各有不同,但是说明的都是同一个问题,在windows中使用过长的路径,超过240字符. Error:Error: File path too long on Windows, keep below 240 characters : C:\Users\admin\Work\3-Code\4-ArcGIS Android Sample Code\ArcGISAndroidQuartzSample\vector