Acceso denegado conseguir cuando intenta borrar archivo
-
28-09-2019 - |
Pregunta
Me estoy poniendo acceso denegado siempre que intente eliminar un archivo después de terminar de leerlo en C:\inetpub\wwwroot\Project\temp\
. Me Close () y botar () el StreamReader adecuadamente ya? También me dio permiso completo para la cuenta de servicio de red? ¿Puede alguien ayudarme?
reader = new StreamReader(path + fileName);
DataTable dt = new DataTable();
String line = null;
int i = 0;
while ((line = reader.ReadLine()) != null)
{
String[] data = line.Split(',');
if (data.Length > 0)
{
if (i == 0)
{
dt.Columns.Add(new DataColumn());
foreach (object item in data)
{
DataColumn c = new DataColumn(Convert.ToString(item));
if (Convert.ToString(item).Contains("DATE"))
{
c.DataType = typeof(DateTime);
}
else { c.DataType = typeof(String); }
dt.Columns.Add(c);
}
dt.Columns.Add(new DataColumn("CreatedDate", typeof(DateTime)));
i++;
}
else
{
DataRow row = dt.NewRow();
row[0] = "";
for (int j = 0; j < data.Length; j++)
{
if (dt.Columns[j + 1].DataType == typeof(DateTime))
{
row[j + 1] = Convert.ToDateTime(data[j]);
}
else
{
row[j + 1] = data[j];
}
}
row[data.Length + 1] = DateTime.Now.ToString();
dt.Rows.Add(row);
}
}
}
DataAccess dataAccess = new DataAccess(Constant.CONNECTION_STRING_NAME);
dataAccess.WriteBulkData(dt, Constant.TABLE);
reader.Close();
reader.Dispose();
File.Delete(path);
Solución
Su llamada al método File.Delete debe tomar ruta + nombre de archivo como parámetro. Esto se debe a que de acuerdo con este enlace http: // MSDN. ruta microsoft.com/en-us/library/system.io.file.delete.aspx es la ruta completa incluyendo el nombre de archivo y la variable de ruta incluye sólo el nombre de la carpeta.
Otros consejos
Se va a eliminar File.Delete(path);
no File.Delete(path + filename);
Está abriendo
reader = new StreamReader(path + fileName);
Pero que está cerrando
File.Delete(path);
También tenía el problema, por lo tanto, me tropiezo en esta entrada al servidor. He añadido la siguiente línea de código antes y después de una copia / borrado.
Eliminar
File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);
Copiar
File.Copy(file, dest, true);
File.SetAttributes(dest, FileAttributes.Normal);
Enlace: ¿Por qué es el acceso a la trayectoria negado / p>