使用已定义的文件命名约定将文件保存到所需位置

本文关键字:文件 保存 位置 命名约定 定义 | 更新日期: 2023-09-27 18:09:30

我想将打开的图片保存到预定义的位置,使用定义的命名约定(如日期+原始名称)。

如何消除被savedialog询问文件名和文件夹的问题?
Image Dosya;
private void btnopen_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "Jpg|*.Jpg";
            if (of.ShowDialog()==DialogResult.OK)
            {
                Dosya = Image.FromFile(of.FileName);
                pictureBox1.Image = Dosya;
            }
        }
 private void btnSave_Click(object sender, EventArgs e)
        {
        SaveFileDialog sd = new SaveFileDialog();
        sd.InitialDirectory = "C:''Users''sonyy''Videos''";
        sd.Title = "Save Files";
        sd.CheckFileExists = true;
        sd.CheckPathExists = true;
        sd.DefaultExt = "jpg";
        sd.Filter = "JPG(*.jpg)|*.jpg|All files (*.*)|*.*";
        sd.FilterIndex = 1;
        sd.RestoreDirectory = false;
        if (sd.ShowDialog() == DialogResult.OK)
        {
            string dosyaadi = sd.InitialDirectory;
            string date = Convert.ToString(DateTime.Today.ToShortDateString());
            sd.FileName = date;
            Dosya.Save(sd.InitialDirectory+date+"."+sd.DefaultExt);
        }
}

使用已定义的文件命名约定将文件保存到所需位置

您不需要打开文件保存对话框。
你知道路径是什么,你知道名字是什么(例如日期时间)。
只需保存文件,而不显示对话框。使用图像。保存图片

Image Dosya;
private void btnopen_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            of.Filter = "Jpg|*.Jpg";
            if (of.ShowDialog()==DialogResult.OK)
            {
                Dosya = Image.FromFile(of.FileName);
                pictureBox1.Image = Dosya;
            }
        }
 private void btnSave_Click(object sender, EventArgs e)
        {
        SaveFileDialog sd = new SaveFileDialog();
        sd.InitialDirectory = "C:''Users''sonyy''Videos''";
        sd.Title = "Save Files";
        sd.CheckFileExists = true;
        sd.CheckPathExists = true;
        sd.DefaultExt = "jpg";
        sd.Filter = "JPG(*.jpg)|*.jpg|All files (*.*)|*.*";
        sd.FilterIndex = 1;
        sd.RestoreDirectory = false;
            string dosyaadi = sd.InitialDirectory;
            string date = Convert.ToString(DateTime.Today.ToShortDateString());
            sd.FileName = date;
            Dosya.Save(sd.InitialDirectory+date+"."+sd.DefaultExt);
 SaveFileDialog sd = new SaveFileDialog();
            sd.InitialDirectory = "C:''Users''sonyy''Videos''";
            sd.Title = "Save Files";
            //sd.CheckFileExists = true;
            sd.CheckPathExists = true;
            sd.DefaultExt = "jpg";
            sd.Filter = "JPG(*.jpg)|*.jpg|All files (*.*)|*.*";
            sd.FilterIndex = 1;
            sd.RestoreDirectory = false;

            if (sd.ShowDialog() == DialogResult.OK)
            {
                string dosyaadi = sd.InitialDirectory;
                string date = Convert.ToString(DateTime.Today.ToShortDateString());
                sd.FileName = date;
                Dosya.Save(date + "." + sd.DefaultExt);
            }