這是我的代碼,我試圖用另一個檔案夾中的另一個影像替換一個檔案夾中的影像,同時保留他的檔案名。我正在使用dataGridView
我正在將影像從選擇的單元格直接加載到picturebox
我正在使用的單元格中檢索檔案夾的內容:
File.Copy(Source, Destination,true);
要復制我的檔案,我確保處理掉所有內容,以便行程不使用它。
我的完整專案(評論):
private void Form1_Load(object sender, EventArgs e)
{
//Get content of the DESTINATION files on a specific folder and put it into a dataGridView1
string[] files = Directory.GetFiles(@Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) "\\Black Desert\\FaceTexture", "*", SearchOption.TopDirectoryOnly);
DataTable table = new DataTable();
table.Columns.Add("File Name");
for (int i = 0; i < files.Length; i )
{
FileInfo file = new FileInfo(files[i]);
table.Rows.Add(file.Name);
}
dataGridView1.DataSource = table;
//Get content of the SOURCE files on a specific folder and put it into a dataGridView2
string[] files2 = Directory.GetFiles(@"Face Texture", "*");
DataTable table2 = new DataTable();
table2.Columns.Add("File Name");
for (int i = 0; i < files2.Length; i )
{
FileInfo file = new FileInfo(files2[i]);
table2.Rows.Add(file.Name);
}
dataGridView2.DataSource = table2;
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
string[] sizes = { "B", "KB", "MB", "GB", "TB" };
double len = new FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) "\\Black Desert\\FaceTexture\\" dataGridView1.SelectedCells[0].Value.ToString()).Length;
int order = 0;
while (len >= 1024 && order < sizes.Length - 1)
{
order ;
len = len / 1024;
}
string result = String.Format("{0:0.##} {1}", len, sizes[order]);
//We get the picturebox to show which file we selected in the cell (datagridview1)
pictureBox1.Image = Image.FromFile(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) "\\Black Desert\\FaceTexture\\" dataGridView1.SelectedCells[0].Value.ToString());
LblFileName1.Text = dataGridView1.SelectedCells[0].Value.ToString();
//We have the full path here
LblFilePath1.Text = (Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) "\\Black Desert\\FaceTexture\\" dataGridView1.SelectedCells[0].Value.ToString());
LblFileSize1.Text = result;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
button1.Enabled = true;
string[] sizes = { "B", "KB", "MB", "GB", "TB" };
double len = new FileInfo("Face Texture\\" dataGridView2.SelectedCells[0].Value.ToString()).Length;
int order = 0;
while (len >= 1024 && order < sizes.Length - 1)
{
order ;
len = len / 1024;
}
string result = String.Format("{0:0.##} {1}", len, sizes[order]);
//We get the picturebox to show which file we selected in the cell (datagridview2)
pictureBox2.Image = Image.FromFile("Face Texture\\" dataGridView2.SelectedCells[0].Value.ToString());
LblFileName2.Text = dataGridView2.SelectedCells[0].Value.ToString();
//We have the full path here
LblFilePath2.Text = "Face Texture\\" dataGridView2.SelectedCells[0].Value.ToString();
LblFileSize2.Text = result;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//Get store into a string both paths before disposing them
string Source = LblFilePath1.Text;
string Destination = LblFilePath2.Text;
pictureBox1.Dispose();
pictureBox2.Dispose();
dataGridView1.Dispose();
dataGridView2.Dispose();
LblFileName1.Dispose();
LblFileName2.Dispose();
LblFilePath1.Dispose();
LblFilePath2.Dispose();
LblFileSize1.Dispose();
LblFileSize2.Dispose();
File.Copy(Source, Destination, true);
MessageBox.Show(Source Destination);
}
catch (Exception ex2)
{
//Eh, used by the process ..
MessageBox.Show(ex2.Message);
}
}
uj5u.com熱心網友回復:
您想要處理影像物件而不是 PictureBox 物件。
因此pictureBox.Image.Dispose()將解決問題。
所以PictureBox有一個影像屬性類,它有一個Dispose()方法
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/457628.html
