我需要使用带有日期选择器的文本框来选择日期。基于选定的日期,当我单击“执行”按钮时,应在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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句