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

时间:2016-09-30 00:24:51

标签: c#

我想将已打开的图片保存到具有已定义命名约定的预定义位置,例如日期+原始名称。如何通过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);
        }
}

3 个答案:

答案 0 :(得分:1)

您无需打开文件保存对话框 你知道路径是什么,你知道名称是什么(例如日期时间) 只需保存文件而不显示对话框。 假设您有图像,请使用Image.Save

答案 1 :(得分:0)

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);

答案 2 :(得分:-1)

 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);

            }