C#代码实现创建文件夹
时间: 2023-11-01 11:40:48 浏览: 52
C是一种计算机编程语言,由Dennis Ritchie在20世纪70年代开发。它是一种高级语言,被广泛用于系统软件、操作系统、网络设备、游戏开发等领域。C语言被认为是现代计算机编程语言的基础之一,它的设计简洁、灵活,具有很好的可移植性。C语言的语法规则和程序结构清晰明了,易于理解和学习,因此成为了计算机科学教育中必学的一门编程语言。
相关问题
C# 根目录创建文件夹
以下是在C#中创建根目录文件夹的示例代码:
```csharp
using System;
using System.IO;
namespace CreateDirectoryExample
{
class Program
{
static void Main(string[] args)
{
string rootPath = @"C:\MyFolder"; // 根目录路径
string folderName = "NewFolder"; // 新文件夹名称
// 判断根目录是否存在
if (!Directory.Exists(rootPath))
{
Console.WriteLine("根目录不存在!");
return;
}
// 创建新文件夹
string newFolderPath = Path.Combine(rootPath, folderName);
Directory.CreateDirectory(newFolderPath);
Console.WriteLine("已在根目录下创建新文件夹:" + newFolderPath);
}
}
}
```
上述代码中,我们首先定义了根目录路径和新文件夹名称,然后使用`Directory.Exists()`方法判断根目录是否存在,如果不存在则直接返回。接着使用`Path.Combine()`方法将根目录路径和新文件夹名称拼接成新的文件夹路径,最后使用`Directory.CreateDirectory()`方法创建新文件夹。
c# FTP 创建文件夹
以下是使用C#创建FTP文件夹的示例代码:
```csharp
using System;
using System.Net;
class Program
{
static void Main(string[] args)
{
string ftpFolderName = "newFolder"; // 要创建的文件夹名称
string ftpServerIP = "ftp://ftp.example.com"; // FTP服务器地址
string ftpUserID = "username"; // FTP登录用户名
string ftpPassword = "password"; // FTP登录密码
try
{
// 创建FTP请求对象
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServerIP + "/" + ftpFolderName);
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
// 发送FTP请求
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("FTP文件夹创建成功!");
response.Close();
}
catch (WebException ex)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{
Console.WriteLine("FTP文件夹已存在!");
}
else
{
Console.WriteLine("FTP文件夹创建失败:" + ex.Message);
}
}
}
}
```
该示例代码使用`FtpWebRequest`类创建FTP请求对象,并设置请求方法为`WebRequestMethods.Ftp.MakeDirectory`,表示创建文件夹。然后设置FTP登录用户名和密码,并发送FTP请求。如果文件夹已存在,则会捕获`WebException`异常,并判断异常状态码是否为`FtpStatusCode.ActionNotTakenFileUnavailable`,如果是,则表示文件夹已存在,否则表示创建文件夹失败。