asp.net mvc - File is a Method which is not valid in the given context -
this question has answer here:
trying check whether file exists inside path getting build error on file.
file method , cannot used in context.
if (!file.exists(excelfilepath)) throw new filenotfoundexception(excelfilepath); if (file.exists(csvoutputfile)) throw new argumentexception("file exists: " + csvoutputfile);
full class code
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); // schema, 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 found in spreadsheet"); string worksheet = schematable.rows[worksheetnumber - 1]["table_name"].tostring().replace("'", ""); string sql = string.format("select * [{0}]", worksheet); var da = new oledbdataadapter(sql, cnn); da.fill(dt); } catch (exception e) { // ??? throw e; } { // 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(); } } }
how can fix this?
you have method inside mvc controller in file method exists. add in code system.io.file
instead of file
Comments
Post a Comment