My app has a Login Form.
Following code which I created a text file is not correct. I don't know why.
Error is Access denied in C:\Windows\Apple.txt
And i want this text file to log usernames when user changed his username
Please help me with correct code.
thx C-SharpCorner guys......
using System.IO;
string fileLocation = @"C:\Windows\Apple.txt";
if (!File.Exists(fileLocation))
{
FileStream xFile = new FileStream(fileLocation, FileMode.CreateNew);
StreamWriter sw = new StreamWriter(xFile);
sw.Write(this.txtUsername.Text);
sw.Close();
}

Sanjeeb LenkaPosted Aug 12, 2013, 5:59 AM
The
C:\windowsfolder is protected (by design) in Vista and Windows 7 (and Windows Server 2008 / 2008 R2) - normal user accounts do not have any permission to create directories and files in there - it's a system folder.Either you need to run as admin - then you have permission to create directories and files even in protected system folders - or you create the directories and file elsewhere and not inside a protected system folder.
Sanjeeb LenkaPosted Aug 12, 2013, 6:27 AM
The File class provides the Encrypt and the Decrypt methods to restrict other users to open and read a file.
Encrypt a File in C#
The Encrypt method encrypts a file so that only the account used to encrypt the file can decrypt it.
string fileName = @"c:\temp\Mahesh.txt";
File.Encrypt(fileName);
Decrypt a File in C#
The Decrypt method decrypts an encrypted file. Only account that has encrypted a file can decrypt a file.
string fileName = @"c:\temp\Mahesh.txt";
File.Decrypt(fileName);
Osayz FamilyPosted Aug 12, 2013, 6:17 AM