2012-04-30 3 views
0

데이터와 이미지를 업로드하려고 할 때 약간의 문제가 있습니다.System.IO.IOException : 프로세스가 'file/file'파일에 액세스 할 수 없습니다.

문제는 다음과 같습니다

System.IO.IOException: The process cannot access the file 'C:\MyWebSites\Class\OriginalImages\IMG_9209.JPG' because it is being used by another process. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode) at System.Web.HttpPostedFile.SaveAs(String filename) at Admin_InsertStudent.Button1_Click(Object sender, EventArgs e) 

C 번호 :

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using System.Text; 

public partial class Admin_InsertStudent : System.Web.UI.Page { 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
     try 
     { 
     if (FileUpload1.HasFile) 
     { 
      string tempPath = "OriginalImages"; 
      string imgPath = "StudentImages"; 
      string savePath = Path.Combine(Request.PhysicalApplicationPath, tempPath); 
      string TempImagePath = Path.Combine(savePath, FileUpload1.FileName); 

      string imgSavePath = Path.Combine(Request.PhysicalApplicationPath, imgPath); 
      string StudentImageNormal = Path.Combine(imgSavePath, FileUpload1.FileName); 
      string StudentImageThumbnail = Path.Combine(imgSavePath, "t__" + FileUpload1.FileName); 
      string extension = Path.GetExtension(FileUpload1.FileName); 

      switch (extension.ToLower()) 
      { 
       case ".png": 
        goto case "Upload"; 
       case ".gif": 
        goto case "Upload"; 
       case ".jpg": 
        goto case "Upload"; 
       case "jpeg": 
        goto case "Upload"; 

       case "Upload": 
        FileUpload1.SaveAs(TempImagePath); 

        ImageTools.GenerateTumbnail(TempImagePath,StudentImageNormal, 400, 300, true, "High"); 

        ImageTools.GenerateTumbnail(TempImagePath, StudentImageThumbnail, 120, 90, true, "medium"); 

        lblMessage.Text = "Billedet" + FileUpload1 + "er nu uploadet"; 

        string FirstName = Convert.ToString(txtFirstName.Text); 
        string MiddleName = Convert.ToString(txtMiddelName.Text); 
        string LastName = Convert.ToString(txtLastName.Text); 
        string BirthDay = Convert.ToString(txtBirthDay.Text); 
        string City = Convert.ToString(txtCity.Text); 
        string Pic = "OriginalImages/StudentImages/" + FileUpload1.FileName; 
        string WebSite = Convert.ToString(txtWebSite.Text); 

        break; 

       default: 
        lblMessage.Text = "Denne Filetype er ikke tilladt"; 
        return; 
      } 

     } 
    } 
    catch (Exception err) 
    { 
     lblMessage.Text = err.ToString(); 
    } 
     } } 

희망 누군가가 당신이 그것을 해제 할 파일을 업로드 한 후 나 :

+0

좋은 서식은 친구입니다. –

답변

1

도움을 줄 수 있습니다. MSDN

사용

yourFile.Dispose(); 
이 방법을 닫거나 파일, 스트림으로 관리되지 않는 리소스를 해제하고, 이 인터페이스를 구현하는 클래스의 인스턴스에 의해 개최 핸들합니다. 이 방법은 규칙에 따라 객체가 보유한 자원을 해제하거나 객체를 재사용하기 위해 준비한 모든 작업에 사용됩니다 ( ).

관련 문제