2013-05-10 25 views
7

Provare a verificare se un file esiste all'interno di un percorso ma riceve un errore di compilazione su File.Il file è un metodo non valido nel contesto specificato

Il file è un metodo e non può essere utilizzato in questo contesto.

if (!File.Exists(excelFilePath)) throw new FileNotFoundException(excelFilePath); 
     if (File.Exists(csvOutputFile)) throw new ArgumentException("File exists: " + csvOutputFile); 

Codice classe completa

static void CovertExcelToCsv(string excelFilePath, string csvOutputFile, int worksheetNumber = 1) 
    { 
     if (!File.Exists(excelFilePath)) throw new FileNotFoundException(excelFilePath); 
     if (File.Exists(csvOutputFile)) throw new ArgumentException("File exists: " + csvOutputFile); 

     // connection string 
     var cnnStr = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;IMEX=1;HDR=NO\"", excelFilePath); 
     var cnn = new System.Data.OleDb.OleDbConnection(cnnStr); 

     // get schema, then data 
     var dt = new DataTable(); 
     try 
     { 
      cnn.Open(); 
      var schemaTable = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 
      if (schemaTable.Rows.Count < worksheetNumber) throw new ArgumentException("The worksheet number provided cannot be found in the spreadsheet"); 
      string worksheet = schemaTable.Rows[worksheetNumber - 1]["table_name"].ToString().Replace("'", ""); 
      string sql = String.Format("select * from [{0}]", worksheet); 
      var da = new OleDbDataAdapter(sql, cnn); 
      da.Fill(dt); 
     } 
     catch (Exception e) 
     { 
      // ??? 
      throw e; 
     } 
     finally 
     { 
      // free resources 
      cnn.Close(); 
     } 

     // write out CSV data 
     using (var wtr = new StreamWriter(csvOutputFile)) 
     { 
      foreach (DataRow row in dt.Rows) 
      { 
       bool firstLine = true; 
       foreach (DataColumn col in dt.Columns) 
       { 
        if (!firstLine) { wtr.Write(","); } else { firstLine = false; } 
        var data = row[col.ColumnName].ToString().Replace("\"", "\"\""); 
        wtr.Write(String.Format("\"{0}\"", data)); 
       } 
       wtr.WriteLine(); 
      } 
     } 
    } 

Come si può risolvere questo problema?

+0

dovrai fornire l'errore –

+0

Ofcourse, sorry editato. – b0w3rb0w3r

risposta

41

Probabilmente questo metodo è presente nel controller MVC in cui esiste il metodo File. Aggiungi il tuo codice System.IO.File invece di File

+1

Questo l'ha risolto grazie. – b0w3rb0w3r

+0

Questo ha risolto anche me ... –

Problemi correlati