我需要使用带日期选择器的文本框来选择日期。根据所选日期,当我点击"去"按钮,应在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)
调用此方法时,它会发出错误
无效的表达式术语字符串
但单击按钮时应创建文件夹。你能告诉我我该怎么办?
答案 0 :(得分:0)
在方法声明中,您指示参数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
}