在 C# 中更改名称和移动文件
Change name and move file in C#
private void btn_add_image_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "Choose a file";
openFileDialog1.InitialDirectory = "C:\";
openFileDialog1.Filter = " JPEG Files (*.jpg;*.jpeg;*.jpe;*.jfif)|*.jpg|All Files (*.*)|*.*";
openFileDialog1.ShowDialog();
string file_name = openFileDialog1.FileName;
string filename2 = openFileDialog1.SafeFileName;
pictureBox1.Image = Image.FromFile(file_name);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
string[] extension = getExtension("images\" + userid);
if (File.Exists("images\" + userid + extension[0]))
{
File.Delete("resimler\" + userid + extension[0]);
}
}
catch (Exception)
{
MessageBox.Show("İmage cannot find");
}
我想更改文件名并保存,所以我在文件存在的情况下编写了这段代码,而不是删除文件并使用用户 ID 名称保存选择的文件,但我无法更改名称并保存文件
if (File.Exists(@"\path\to\source"))
{
File.Move(@"\path\to\source",@"\path\to\destination")
}
我想我可以用这段代码解决你的两个问题。
System.IO.File.Move("old_file_name_path", "new_file_name_path");
这会将文件移动到新文件名。看这里:File.Move
但是,我真的不明白你在这里问什么:
i wrote this code if file exists , than delete file and save the
choosen with userid name but i cant do change name and save file
你能说得具体点吗?
谢谢大家
private void btn_save_Click(object sender, EventArgs e)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
string source = openFileDialog1.FileName;
string[] extension = getExtension(source);
string destination = "images\" + userid + extension[0];
System.IO.File.Move(source, destination);
pictureBox1.Image = Image.FromFile("images\" + userid + extension[0]);
}
private void btn_add_image_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "Choose a file";
openFileDialog1.InitialDirectory = "C:\";
openFileDialog1.Filter = " JPEG Files (*.jpg;*.jpeg;*.jpe;*.jfif)|*.jpg|All Files (*.*)|*.*";
openFileDialog1.ShowDialog();
string file_name = openFileDialog1.FileName;
string filename2 = openFileDialog1.SafeFileName;
pictureBox1.Image = Image.FromFile(file_name);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
string[] extension = getExtension("images\" + userid);
if (File.Exists("images\" + userid + extension[0]))
{
File.Delete("resimler\" + userid + extension[0]);
}
}
catch (Exception)
{
MessageBox.Show("İmage cannot find");
}
我想更改文件名并保存,所以我在文件存在的情况下编写了这段代码,而不是删除文件并使用用户 ID 名称保存选择的文件,但我无法更改名称并保存文件
if (File.Exists(@"\path\to\source"))
{
File.Move(@"\path\to\source",@"\path\to\destination")
}
我想我可以用这段代码解决你的两个问题。
System.IO.File.Move("old_file_name_path", "new_file_name_path");
这会将文件移动到新文件名。看这里:File.Move
但是,我真的不明白你在这里问什么:
i wrote this code if file exists , than delete file and save the choosen with userid name but i cant do change name and save file
你能说得具体点吗?
谢谢大家
private void btn_save_Click(object sender, EventArgs e)
{
pictureBox1.Image.Dispose();
pictureBox1.Image = null;
string source = openFileDialog1.FileName;
string[] extension = getExtension(source);
string destination = "images\" + userid + extension[0];
System.IO.File.Move(source, destination);
pictureBox1.Image = Image.FromFile("images\" + userid + extension[0]);
}