我試圖使用File.Copy()函數將一個圖像從一個位置複製到另一個位置,但它使進程無法訪問異常,任何一個可以請幫忙在這個波紋是代碼塊。我已經附上截圖的例外。該進程無法訪問該文件,因爲它正在被另一個進程使用。##
private void btnUpload_Click(object sender, EventArgs e)
{
string SourcePath;
string RootDrive;
string DestPath;
string fileName;
fileName = "";
try
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Select Image to Upload";
ofd.Filter = "Jpg|*.jpg|Jpge|*.jpge|Gif|*.gif";
ofd.FileName = null;
if (ofd.ShowDialog() != DialogResult.Cancel)
{
fileName = ofd.FileName;
}
ofd.Dispose();
DestPath = Directory.GetCurrentDirectory() + @"\Uploads\PropertyImages\";
string destFile = System.IO.Path.Combine(DestPath, fileName);
if (!System.IO.Directory.Exists(DestPath))
{
System.IO.Directory.CreateDirectory(DestPath);
}
System.IO.File.Copy(fileName, destFile, true);
}
catch (Exception ae)
{
MessageBox.Show(ae.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
一些其他程序正在使用的文件。就這麼簡單。你有沒有在另一個程序副本中打開文件?其他一些程序? – siride
此外,如果您將文件上傳到當前位置的相同位置,則會出現該錯誤。確保您的源路徑和目標路徑不同。 – siride
@siride否該文件未被其他程序使用,並且是兩個路徑都不相同 – Shashikant