2013-05-17 68 views
0

我有以下錯誤:ASP.net進程無法訪問文件,因爲它正被另一個進程使用

The process cannot access the file because it is being used by another process. 

試圖保存文件,然後閱讀它,請你能幫助我解決代碼 ?

protected void Button1_Click(object sender, EventArgs e) 
{ 
    string path = "\\\\nasouts01\\CodiceDiRete\\temp\\"; 



    if (FileUpload1.HasFile) 
    { 
     string fileExt = 
      System.IO.Path.GetExtension(FileUpload1.FileName); 

     if (fileExt == ".csv") 
     { 
      try 
      { 

       FileUpload1.SaveAs(path + FileUpload1.FileName); 

      } 
      catch (Exception ex) 
      { 
       Label1.Text = "ERROR: " + ex.Message.ToString(); 
      } 
      finally 
      { 
       FileUpload1.PostedFile.InputStream.Flush(); 
       FileUpload1.PostedFile.InputStream.Close(); 
       FileUpload1.FileContent.Dispose();  
       //Release File from Memory after uploading 
      } 
     } 
     else 
     { 
      Label1.Text = "Selezionare soltanto file .csv"; 
      gwreportpub.Visible = false; 
      ButtonConferma.Visible = false; 
      ButtonAnnulla.Visible = false; 
      ButtonPulisci.Visible = false; 
      return; 
     } 
    } 
    else 
    { 
     Label1.Text = "Inserire un file "; 
     gwreportpub.Visible = false; 
     ButtonConferma.Visible = false; 
     ButtonAnnulla.Visible = false; 
     ButtonPulisci.Visible = false; 
     return; 
    } 
    /* 
    Validazione nel file , dopo visualizzazione dei dati nella tabella 
    */ 
    int count = 0; 
    string line; 

    // Read the file and display it line by line. 
    using (System.IO.StreamReader file = 
    new System.IO.StreamReader(path+FileUpload1.FileName)) 
    { 
    while((line = file.ReadLine()) != null) 
    { 
     count = line.Split(',').Length; 
     if(count != 12){ 

     Label1.Text = "Il file non contiene il numero esatto di campi che servono al caricamento"; 
     gwreportpub.Visible = false; 
     ButtonConferma.Visible = false; 
     ButtonAnnulla.Visible = false; 
     ButtonPulisci.Visible = false; 
     return; 

     } 
    } 

    file.Close(); 
    } 
    /* 
    Prendo il valore della sequence 
    */ 
    OracleCommand sequenceCmd = new OracleCommand("select ls_seq_batchrequest.nextval from dual",connection); 
    OracleDataReader reader = sequenceCmd.ExecuteReader(); 
    int sequenceVal=0; 
    while (reader.Read()) 
    { 
     //dropdownlist1.Items.Insert(0, new ListItem(reader.GetString(0), reader.GetString(0))); 
     sequenceVal = reader.GetInt32(0); 
    } 
    reader.Close(); 

    Session["sequenceVal"] = sequenceVal; 

    /* 
    Faccio l'inserimento a db dei dati e visualizzo la tabella 
    */ 
    OracleTransaction myTrans = null; 


    OracleCommand cmd = new OracleCommand("INSERT INTO ZTMP_SAM_TB_ELAB_PDR(facilityid,taxid,plantcode,ca_pdr, fullmeterkey_m1, corrmeterid_m1, " + 
    "stopreading_m1, corrstopreading_m1,fullmeterkey_m2, corrmeterid_m2, " + 
    "stopreading_m2, corrstopreading_m2, stopreadtime_m1,stopreadtime_m2,uidbatchrequest) " + 
    " VALUES (:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,:13,:14,:15)", connection); 
    //myTrans = connection.BeginTransaction(); 
    //cmd.Transaction = myTrans; 
    /* 
    (:facilityid,:taxid, 
    :plantcode,:capdr,:fullmetkeym2, 
    :corrmetidm2,:stopreadingm2, 
    :corrstopreadm2,:fullmetkeym1, 
    :corrmetidm1,:stopreadingm1,:corrstopreadm1, 
    :stopreadtimem2,:stopreadtimem1) 
    */ 
    using (System.IO.StreamReader file2 = 
    new System.IO.StreamReader(path+FileUpload1.FileName)) 
    { 
    //System.IO.StreamReader file2 = new System.IO.StreamReader(path+FileUpload1.FileName); 
    try 
    { 
    while((line = file2.ReadLine()) != null) 
    { 
     string[] split = line.Split(','); 


     cmd.Parameters.Add("1", OracleType.VarChar, 64).Value = split[0]; 
     cmd.Parameters.Add("2", OracleType.VarChar, 64).Value = ""; 
     cmd.Parameters.Add("3", OracleType.VarChar, 64).Value = ""; 
     cmd.Parameters.Add("4", OracleType.Number).Value = Convert.ToInt32(split[1]); 
     cmd.Parameters.Add("5", OracleType.VarChar, 64).Value = split[6]; 
     cmd.Parameters.Add("6", OracleType.VarChar, 64).Value = split[7]; 
     cmd.Parameters.Add("7", OracleType.Number).Value = Convert.ToInt32(split[8]); 
     cmd.Parameters.Add("8", OracleType.Number).Value = Convert.ToInt32(split[9]); 
     cmd.Parameters.Add("9", OracleType.VarChar, 80).Value = split[2]; 
     cmd.Parameters.Add("10", OracleType.VarChar, 80).Value = split[3]; 
     cmd.Parameters.Add("11", OracleType.Number).Value = Convert.ToInt32(split[4]); 
     cmd.Parameters.Add("12", OracleType.Number).Value = Convert.ToInt32(split[5]); 
     DateTime date1,date2; 

     DateTime.TryParseExact(split[10], "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out date1); 
     DateTime.TryParseExact(split[11], "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out date2); 

     cmd.Parameters.Add("13", OracleType.DateTime).Value = date1; 
     cmd.Parameters.Add("14", OracleType.DateTime).Value = date2; 
     cmd.Parameters.Add("15", OracleType.Number).Value = sequenceVal; 

     cmd.ExecuteNonQuery(); 


     /*Per ogni elemento esegui l'insert*/ 
    } 
    //myTrans.Commit(); 
    } 
    catch(Exception ex){ 

     //myTrans.Rollback(); 
     Label1.Text = ex.Message; 
     return; 
    //"Si e' verificato un errore nell'inserimento dei dati nella tabella ZTMP_SAM_TB_ELAB_PDR"; 
    } 


    file2.Close(); 
    } 


    } 
+0

是大的方法,我建議你破解過程分成若干更小的方法,它會更容易調試從那裏 – RoughPlace

+0

做我做到了.. –

+0

只是一個尖無關您當前的問題:在c#中使用字符串文字來消除逃離路徑的需要。例如:'string path = @「\\ nasouts01 \ CodiceDiRete \ temp \」;'http://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx – BLSully

回答

0

你可以做兩件事情來解決這個

  1. 保存與時間戳文件中的姓名和工作在該副本

  2. 使用File.ReadLine方法,如果文件是CSV。

+0

什麼意思是使用File.ReadLine方法,如果你是CSV文件。 ?? –

相關問題