Hey;
I am developing a console game in C#. I wanted to create a file called "accounts.bin" to hold players accounts information.
So when I try to create or access the file the program halts and throws an exception called "UnauthroizedAccessException". I tried to put the file into the "C:\" directory, then "C:\ProgramFiles(x86)\" directory but nothing changed.
Here below is my AccountsFile class, I just implemented the constructors. How can I grant the application with the needed authorization ? Is there anything wrong with the code, because this is the first time I am working with File IO.
public class AccountsFile
{
#region Fields
string _fileName;
int _numberOfAccounts;
#endregion
#region Properties
public string FileName { get; set; }
public int NumberOfAccounts { get; set; }
public DateTime LastModifiedDate { get; set; }
#endregion
#region Constructors
public AccountsFile()
{
NumberOfAccounts = 0;
FileName = "\\Program Files (x86)\\BrainSalad\\info\\accounts.bin";
BinaryWriter file = new BinaryWriter(File.Create(FileName));
file.Write(NumberOfAccounts);
}
public AccountsFile(string iFileName)
{
FileName = iFileName;
try
{
BinaryReader file = new BinaryReader(File.Open(FileName, FileMode.Open));
}
catch (Exception ex)
{
BinaryWriter file = new BinaryWriter(File.Create(FileName));
file.Write(0);
}
BinaryReader existingFile = new BinaryReader(File.Open(FileName, FileMode.Open));
NumberOfAccounts = existingFile.ReadInt32();
}
#endregion
}