#c# #winforms
#c# #winforms
Вопрос:
Я хочу, чтобы, когда пользователь нажимал на кнопку и открывал изображение, изображение копировалось в другое место, а путь к файлу скопированного изображения сохранялся в свойствах.Настройки.Default.custombgfilepath’ Вот мой код:
private void Button2_Click(object sender, EventArgs e)
{
//check for openfile dialog result
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
//initialize destination
string destination = @"C:Launchicity";
//Get filename
string filename = Path.GetFileName(openFileDialog1.FileName);
//get filepath
string filepath = destination filename;
if (!File.Exists(filepath))
{
File.Copy(openFileDialog1.FileName, filepath);
Properties.Settings.Default.custombgfilepath = filepath;
Properties.Settings.Default.Save();
}
else
{
Properties.Settings.Default.custombgfilepath = filepath;
Properties.Settings.Default.Save();
}
}
}
Вот автоматически созданный дизайнером форм Windows код для openfiledialog1:
//
// openFileDialog1
//
this.openFileDialog1.Filter = ""PNG (*.png)|*.png|JPG (*.jpg)|*.jpg"";
this.openFileDialog1.Title = "Browse Image";
this.openFileDialog1.FileOk = new System.ComponentModel.CancelEventHandler(this.OpenFileDialog1_FileOk);
Однако, когда я нажимаю на кнопку, приложение зависает.
Не могли бы вы мне помочь?
Спасибо
Комментарии:
1. Отображается ли диалоговое окно? Приложение должно зависать до тех пор, пока отображается диалоговое окно.
2. Возможно, было бы неплохо отладить приложение и посмотреть, не выдается ли какая-либо ошибка.
3. К ВАШЕМУ сведению, посмотрите путь. Combine() для построения пути из папки и файла
4. @theblackips диалоговое окно не отображается. Приложение просто зависает.
5. @Apollo199999999 Можем ли мы тогда увидеть часть кода диалогового окна?
Ответ №1:
У меня есть еще вопросы. Пожалуйста, пришлите мне отзыв;) попробуйте это:
(Отредактировано)
using system.IO
private void button1_Click(object sender, EventArgs e)
{
//check for openfile dialog result
this.openFileDialog1.Filter = ""PNG (*.png)|*.png|JPG (*.jpg)|*.jpg"";
this.openFileDialog1.Title = "Browse Image";
openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
textBox1.Text = openFileDialog1.FileName;
//initialize destination
string destination = @"C:Launchicity";
//Get filename
string filename = Path.GetFileName(openFileDialog1.FileName);
//get filepath
string filepath = destination filename;
if (!File.Exists(filepath))
{
File.Copy(openFileDialog1.FileName, filepath);
//Properties.Settings.Default.custombgfilepath = filepath; i dont have custombgfilepath
Properties.Settings.Default.Save();
}
else
{
//Properties.Settings.Default.custombgfilepath = filepath; i dont have custombgfilepath
Properties.Settings.Default.Save();
}
}
Комментарии:
1. Работает! Спасибо