如果初始目录不存在,则创建它,但如果用户取消保存,则删除新添加的文件夹
If initial directory doesn't exist, create it, but if user cancel the save, delete the newly added folders
我想使用 SaveFileDialog 将文本框文本保存到在特殊目录中创建的 .txt 文件。
但是假设我的路径不存在:我希望当对话框出现询问用户要将其 .txt 文件保存到哪里时,对话框会自动创建丢失的文件夹(如果丢失)。但我也喜欢如果用户取消他的保存,新创建的文件夹如果为空则将被删除。
换句话说:SaveFileDialog 对话框显示在初始目录中,但如果初始目录为空,我的代码会生成此目录,但如果用户取消,我的代码会删除生成的目录。
这是我的示例:我想将我的 .txt 保存在 Desktop\FolderExistingOrNot 中,但是如果文件夹 FolderExistingOrNot 我想创建它。但是如果用户取消,如果FolderExistingOrNot为空我想删除。
private void btn_SAVE_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "txt";
sfd.Filter = ".TXT (*.txt)|*.txt";
sfd.FileName = textBox1.Text;
sfd.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\FolderExistingOrNot";
//Directory.CreateDirectory(sfd.InitialDirectory); // could use that but if the user cancel, this folder will not be destroyed
if (sfd.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(sfd.FileName, textBox2.Text);
}
else // if the user cancel the saving
{
// I would like to erase the folder FolderExistingOrNot if it's empty
}
}
这可能很简单,但我还没想好怎么做。
这在我测试时对我有用。
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "txt";
sfd.Filter = ".TXT (*.txt)|*.txt";
sfd.FileName = textBox1.Text;
string mypath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\FolderExistingOrNot";
Directory.CreateDirectory(mypath);
sfd.InitialDirectory = mypath;
//Directory.CreateDirectory(sfd.InitialDirectory); // could use that but if the user cancel, this folder will not be destroyed
if (sfd.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(sfd.FileName, textBox2.Text);
}
else // if the user cancel the saving
{
if (Directory.GetFiles(mypath).Length == 0)
{
Directory.Delete(mypath);
}
}
我想使用 SaveFileDialog 将文本框文本保存到在特殊目录中创建的 .txt 文件。
但是假设我的路径不存在:我希望当对话框出现询问用户要将其 .txt 文件保存到哪里时,对话框会自动创建丢失的文件夹(如果丢失)。但我也喜欢如果用户取消他的保存,新创建的文件夹如果为空则将被删除。
换句话说:SaveFileDialog 对话框显示在初始目录中,但如果初始目录为空,我的代码会生成此目录,但如果用户取消,我的代码会删除生成的目录。
这是我的示例:我想将我的 .txt 保存在 Desktop\FolderExistingOrNot 中,但是如果文件夹 FolderExistingOrNot 我想创建它。但是如果用户取消,如果FolderExistingOrNot为空我想删除。
private void btn_SAVE_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "txt";
sfd.Filter = ".TXT (*.txt)|*.txt";
sfd.FileName = textBox1.Text;
sfd.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\FolderExistingOrNot";
//Directory.CreateDirectory(sfd.InitialDirectory); // could use that but if the user cancel, this folder will not be destroyed
if (sfd.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(sfd.FileName, textBox2.Text);
}
else // if the user cancel the saving
{
// I would like to erase the folder FolderExistingOrNot if it's empty
}
}
这可能很简单,但我还没想好怎么做。
这在我测试时对我有用。
SaveFileDialog sfd = new SaveFileDialog();
sfd.DefaultExt = "txt";
sfd.Filter = ".TXT (*.txt)|*.txt";
sfd.FileName = textBox1.Text;
string mypath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\FolderExistingOrNot";
Directory.CreateDirectory(mypath);
sfd.InitialDirectory = mypath;
//Directory.CreateDirectory(sfd.InitialDirectory); // could use that but if the user cancel, this folder will not be destroyed
if (sfd.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(sfd.FileName, textBox2.Text);
}
else // if the user cancel the saving
{
if (Directory.GetFiles(mypath).Length == 0)
{
Directory.Delete(mypath);
}
}