Obtuve un programa que escribe algunos datos en un archivo usando un método como el siguiente.¿Cómo verifica los permisos para escribir en un directorio o archivo?
public void ExportToFile(string filename)
{
using(FileStream fstream = new FileStream(filename,FileMode.Create))
using (TextWriter writer = new StreamWriter(fstream))
{
// try catch block for write permissions
writer.WriteLine(text);
}
}
Cuando se ejecuta el programa me sale un error:
Unhandled Exception: System.UnauthorizedAccessException: Access to the path 'mypath' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, nt32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions ptions, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolea bFromProxy)
Pregunta: ¿Qué código Qué necesito para coger esto y cómo puedo permitir el acceso?
Esta solución no funciona, SecurityManager.IsGranted devuelve verdadero a pesar de que no tengo permiso de escritura en el archivo (Win XP SP3) –
¿Está ejecutando como administrador local o como un usuario menos privilegiado que no tiene acceso? – Josh
SecurityManager.IsGranted se atribuye como Obsoleto. Esta respuesta podría no funcionar para siempre. – MarkPflug