[英]Creating Sub Folders in FTP Folder in C# Windows Applications
我需要使用带有日期选择器的文本框来选择日期。 基于选定的日期,当我单击“执行”按钮时,应在FTP文件夹中为文本框中指定的日期,月份和年份创建子文件夹。
这应该使用Windows应用程序完成。 你能告诉我我该怎么做吗?
下面是使用日期选择器选择日期并在消息框中显示消息的代码。 但是,除了显示消息外,我需要在FTP文件夹中为文本框中显示的该日期月份和年份创建子文件夹。
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dateTimePicker1.Format = DateTimePickerFormat.Short;
dateTimePicker1.Value = DateTime.Today;
}
private void button1_Click(object sender, EventArgs e)
{
DateTime iDate;
iDate = dateTimePicker1.Value;
MessageBox.Show("Selected date is " + iDate);
}
并在FTP服务器中创建文件夹,我使用的代码是
using System.Net;
private void CreateDirectoryFTP(string directory)
{
string path = @"/" + directory;
WebRequest request = WebRequest.Create(FtpHost + path);
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Credentials = new NetworkCredential(FtpUser, FtpPass);
try
{
request.GetResponse();
}
catch (Exception e)
{
//directory exists
}
}
这在button1_click
事件下使用,当我以这种方式在button1_click
事件中调用此方法CreateDirectoryFTP(string directory)
它给出错误
无效的表达式术语字符串
但是当我单击按钮时应该创建文件夹。 您能告诉我该怎么做吗?
在方法声明中,您指示参数dictionary
的类型为string
。 当您想在button1_click
事件中调用方法时,只需传递任何string
而无需再次使用术语string
。
例:
private void button1_click(object sender, EventArgs e)
{
string directory = @"\Path\You\Want\To\Pass";
DateTime iDate;
iDate = dateTimePicker1.Value;
CreateDirectoryFTP(directory, iDate); // You directly pass the variable to the method
}
或者,如果您不想使用变量来传递路径,则可以直接传递它:
private void button1_click(object sender, EventArgs e)
{
DateTime iDate;
iDate = dateTimePicker1.Value;
CreateDirectoryFTP(@"\Path\You\Want\To\Pass", iDate);
}
要创建子文件夹,请将日期传递给方法CreateDirectoryFTP
:
private void CreateDirectoryFTP(string directory, DateTime date)
{
string path = @"/" + directory;
WebRequest request = WebRequest.Create(FtpHost + path);
request.Method = WebRequestMethods.Ftp.MakeDirectory;
request.Credentials = new NetworkCredential(FtpUser, FtpPass);
try
{
request.GetResponse();
}
catch (Exception e)
{
//directory exists
}
string pathToDay = path + "/" + date.Day.ToString();
// And now you can create the subfolder like you did it for the main folder
string pathToMonth = path + "/" + date.Month.ToString();
// Also with the month and the year you can do it like you did it for the main folder
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.