首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

FTP上传时检查文件是否存在,是否在C#中重命名

在 C# 中,可以使用 System.IO 命名空间中的 FileDirectory 类来操作文件系统。

要检查文件是否存在,可以使用 File.Exists() 方法,例如:

代码语言:csharp
复制
string filePath = "example.txt";
if (File.Exists(filePath))
{
    Console.WriteLine("文件存在");
}
else
{
    Console.WriteLine("文件不存在");
}

要重命名文件,可以使用 File.Move() 方法,例如:

代码语言:csharp
复制
string oldFilePath = "example.txt";
string newFilePath = "example_new.txt";
File.Move(oldFilePath, newFilePath);

注意,在重命名文件时,请确保新文件名已存在,否则会出现异常。如果新文件名已存在,可以使用 File.Replace() 方法替换旧文件名和新文件名,例如:

代码语言:csharp
复制
string oldFilePath = "example.txt";
string newFilePath = "example_new.txt";
File.Replace(oldFilePath, newFilePath, null);

以上是 C# 中操作文件系统的一些基本方法,希望能对你有所帮助。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

  • 领券