既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
try
{
// Create the file, or overwrite if the file exists.
using (FileStream fs = File.Create(path))
{
byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
// Open the stream and read it back.
using (StreamReader sr = File.OpenText(path))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
Create(String, Int32, FileOptions) 创建或覆盖指定路径中的文件,指定缓冲区大小和一个描述如何创建或覆盖该文件的选项
public static System.IO.FileStream Create (string path, int bufferSize, System.IO.FileOptions options);
参数
path
string
要创建的文件的路径及名称。
bufferSize
int
用于读取和写入到文件的已放入缓冲区的字节数。
options
FileOptions 值之一,它描述如何创建或覆盖该文件。
返回
具有指定缓冲区大小的新文件。
示例
以下示例演示如何在创建文件流时使用异步值。
using System;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
WriteToFile();
}
static async void WriteToFile()
{
byte[] bytesToWrite = Encoding.Unicode.GetBytes("example text to write");
using (FileStream createdFile = File.Create("c:/Temp/testfile.txt", 4096, FileOptions.Asynchronous))
{
await createdFile.WriteAsync(bytesToWrite, 0, bytesToWrite.Length);
}
}
}
}
Delete(String) 删除指定的文件
public static void Delete (string path);
参数
path
string
要删除的文件的名称。 不支持通配符。
示例
以下示例将文件组复制到 C:\archives\2008 备份文件夹,然后从源文件夹中将其删除。
string sourceDir = @"c:\current";
string backupDir = @"c:\archives\2008";
try
{
string[] picList = Directory.GetFiles(sourceDir, "*.jpg");
string[] txtList = Directory.GetFiles(sourceDir, "*.txt");
// Copy picture files.
foreach (string f in picList)
{
// Remove path from the file name.
string fName = f.Substring(sourceDir.Length + 1);
// Use the Path.Combine method to safely append the file name to the path.
// Will overwrite if the destination file already exists.
File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true);
}
// Copy text files.
foreach (string f in txtList)
{
// Remove path from the file name.
string fName = f.Substring(sourceDir.Length + 1);
try
{
// Will not overwrite if the destination file already exists.
File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName));
}
// Catch exception if the file was already copied.
catch (IOException copyError)
{
Console.WriteLine(copyError.Message);
}
}
// Delete source files that were copied.
foreach (string f in txtList)
{
File.Delete(f);
}
foreach (string f in picList)
{
File.Delete(f);
}
}
catch (DirectoryNotFoundException dirNotFound)
{
Console.WriteLine(dirNotFound.Message);
}
Exists(String) 确定指定的文件是否存在
public static bool Exists (string? path);
参数
path
string
文件或目录的路径。
返回
bool
如果
path
指向现有目录,则为true
;如果该目录不存在或者在尝试确定指定目录是否存在时出错,则为false
。
示例
下面的示例确定文件是否存在。
string curFile = @"c:\temp\test.txt";
Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist.");
GetCreationTime(String) 返回指定文件或目录的创建日期和时间
public static DateTime GetCreationTime (string path);
参数
path
string
目录的路径。
返回
一个设置为指定目录的创建日期和时间的结构。 该值用本地时间表示。
示例
下面的示例演示了 GetCreationTime
。
using System;
using System.IO;
class Test
{
public static void Main()
{
try
{
// Get the creation time of a well-known directory.
DateTime dt = Directory.GetCreationTime(Environment.CurrentDirectory);
// Give feedback to the user.
if (DateTime.Now.Subtract(dt).TotalDays > 364)
{
Console.WriteLine("This directory is over a year old.");
}
else if (DateTime.Now.Subtract(dt).TotalDays > 30)
{
Console.WriteLine("This directory is over a month old.");
}
else if (DateTime.Now.Subtract(dt).TotalDays <= 1)
{
Console.WriteLine("This directory is less than a day old.");
}
else
{
Console.WriteLine("This directory was created on {0}", dt);
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
Move(String, String) 将指定文件移到新位置,提供要指定新文件名的选项
public static void Move (string sourceFileName, string destFileName);
参数
sourceFileName
string
要移动的文件的名称。 可以包括相对或绝对路径。
destFileName
string
文件的新路径和名称。
示例
下面的示例将移动一个文件。
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
string path2 = @"c:\temp2\MyTest.txt";
try
{
if (!File.Exists(path))
{
// This statement ensures that the file is created,
// but the handle is not kept.
using (FileStream fs = File.Create(path)) {}
}
// Ensure that the target does not exist.
if (File.Exists(path2))
File.Delete(path2);
// Move the file.
File.Move(path, path2);
Console.WriteLine("{0} was moved to {1}.", path, path2);
// See if the original exists now.
if (File.Exists(path))
{
Console.WriteLine("The original file still exists, which is unexpected.");
}
else
{
Console.WriteLine("The original file no longer exists, which is expected.");
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
Open(String, FileMode) 通过不共享的读/写访问权限打开指定路径上的 FileStream
public static System.IO.FileStream Open (string path, System.IO.FileMode mode);
参数
path
string
要打开的文件。
mode
FileMode 值,用于指定在文件不存在时是否创建该文件,并确定是保留还是覆盖现有文件的内容。
返回
FileStream
以读/写访问与不共享权限打开的指定模式和路径上的 FileStream
示例
下面的代码示例创建一个临时文件并向其中写入一些文本。 然后,该示例使用 T:System.IO.FileMode.Open 打开该文件;也就是说,如果该文件不存在,则不会创建它。
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
// Create a temporary file, and put some data into it.
string path = Path.GetTempFileName();
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.None))
{
Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
// Open the stream and read it back.
using (FileStream fs = File.Open(path, FileMode.Open))
{
byte[] b = new byte[1024];
UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(b,0,b.Length) > 0)
{
Console.WriteLine(temp.GetString(b));
}
}
}
}
Replace(String, String, String) 使用其他文件的内容替换指定文件的内容,这一过程将删除原始文件,并创建被替换文件的备份
public static void Replace (string sourceFileName, string destinationFileName, string? destinationBackupFileName);
参数
sourceFileName
string
替换由
destinationFileName
指定的文件的文件名。
destFileName
string
被替换文件的名称。
destinationBackupFileName
string
备份文件的名称。
示例
下面的代码示例使用 Replace 方法将文件替换为其他文件,并创建被替换文件的备份。
using System;
using System.IO;
namespace FileSystemExample
{
class FileExample
{
public static void Main()
{
try
{
string OriginalFile = "test.xml";
string FileToReplace = "test2.xml";
string BackUpOfFileToReplace = "test2.xml.bac";
Console.WriteLine("Move the contents of " + OriginalFile + " into " + FileToReplace + ", delete " + OriginalFile +
", and create a backup of " + FileToReplace + ".");
// Replace the file.
ReplaceFile(OriginalFile, FileToReplace, BackUpOfFileToReplace);
Console.WriteLine("Done");
}
catch (Exception e)



**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.youkuaiyun.com/topics/618545628)**
", and create a backup of " + FileToReplace + ".");
// Replace the file.
ReplaceFile(OriginalFile, FileToReplace, BackUpOfFileToReplace);
Console.WriteLine("Done");
}
catch (Exception e)
[外链图片转存中...(img-73ui79ST-1715752766348)]
[外链图片转存中...(img-VJNjxW9l-1715752766348)]
[外链图片转存中...(img-dqj7GQh3-1715752766348)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.youkuaiyun.com/topics/618545628)**