Reputation: 4169
I need to lock my password dictionary file, i want to lock file from being deleted and changed.
How should i do it? In C# WinApi
or any thing else?
Upvotes: 3
Views: 708
Reputation: 10054
If you are looking for a WINAPI alternative, see this answer. It actually addresses problem that could encountered using FileStream.Lock
.
Upvotes: 0
Reputation: 3638
You can open file with FileShare.None and keep it as long as you want it to be locked
var stream = new FileStream("file.ext",FileMode.Open,
FileAccess.ReadWrite,FileShare.None);
//file is locked
stream.Close(); //lock is release.
http://msdn.microsoft.com/en-us/library/5h0z48dh.aspx
Upvotes: 2
Reputation: 10367
While your program is running you can use FileStream.Lock
and FileStream.UnLock
for locking the file.
Upvotes: 2
Reputation: 3929
why don't you set security on your file (allowing only users you want) and in your code use impersonation. Impersonation will allow your application to use user credentials to access your file
use Impersonator class from url http://www.codeproject.com/Articles/10090/A-small-C-Class-for-impersonating-a-User
using (new Impersonator("Username", "Domainname", "Password"))
{
string pwdfile= File.ReadAllText("c:\dictionaryfile.pwd");
Console.WriteLine(pwdfile);
}
Upvotes: 1