所以我一直在寻找答案,我只是开始使用c#,所以它可能很简单,但是我有一段代码
private void btnAdd_Click(object sender, EventArgs e)
{
using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "TextDocuments|*.txt", ValidateNames = true })
{
if (sfd.ShowDialog() == DialogResult.OK)
{
using (StreamWriter sw = new StreamWriter(sfd.FileName))
{
sw.WriteLineAsync(txtMessage.Text);
MessageBox.Show("Your entry has been saved successfully!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
我想知道在此代码中如何将文件名预设为日期?
答案 0 :(得分:1)
在保存文件对话框实例上将当前的DateTime分配给DefaultFileName
属性
sfd.DefaultFileName=DateTime.Now.ToString();
或
sfd.FileName=DateTime.Now.ToString();
答案 1 :(得分:0)
string logPath = string.Format("{0:yyyy-MM-dd}.txt", DateTime.Now);
Console.WriteLine("Path: " + logPath);
using (StreamWriter file = new StreamWriter(@"./" + logPath, true))
{
file.Write(string.Format("{0} {1} {2} {3}", DateTime.Now, log, Environment.NewLine, bankLog));
}
Console.WriteLine("Logs inserted to file");