Я создаю простое приложение C#, которое должно писать пользовательский ввод в файл.Ошибка C# и File.WriteAllText
Моя первая проблема заключается в том, что я хочу, чтобы пользователь мог установить путь к файлу настроек, но я хочу, чтобы программа могла читать файл настроек при следующих запусках.
Моя вторая проблема заключается в том, что если я использую что-то вроде:
File.WriteAllText(@"C:\Users\Public\settings.settings", message);
я получаю следующее сообщение об ошибке:
System.Security.SecurityException: Request for the permission of type
'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' failed. at
System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark&
stackMark, Boolean isPermSet) at System.Security.CodeAccessPermission.Demand()
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access,
Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions
options, 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, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access,
FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding)
at System.IO.File.WriteAllText(String path, String contents, Encoding encoding)
at IMDBGrabber.frmSettings.writeToFile(String p)
The action that failed was: Demand
The type of the first permission that failed was: System.Security.Permissions.FileIOPermission
The Zone of the assembly that failed was: MyComputer
Любые идеи?
Проверьте разрешение целевой папки на пользователя, запускающего приложение. Используйте try catch, чтобы уловить SecurityException. –