I'm working on a program that acts much like dropbox but locally for my business. I have it setup where some users can't write to some files/directories but they can still write locally. It's not a huge problem, I can just redownload the file/directory but it would be nice if I could just lock it. (they would still need read though)
-
Are you familiar with other program which prevents deletion of things in your computer? including Windows? Because I'm not. – gdoron Nov 16 '12 at 01:15
-
1@gdoron: Simply keeping a file open is enough to lock it. – Tamara Wijsman Nov 16 '12 at 01:15
-
1I think it's something you can try to do with group policies in AD – RAS Nov 16 '12 at 01:16
-
@TomWijsman. But this is not what he wants to do. – gdoron Nov 16 '12 at 01:17
-
Can your application use some sort of SQL Server to check in a table whether the file can be deleted or not, and then handle in code when the user tries to delete? – Dave Lucre Nov 16 '12 at 01:17
-
@TomWijsman would this be efficient with potentially hundreds of files? – David Nov 16 '12 at 01:18
-
@gdoron: Did I say that he should do that, I did not. It's an example counter-case for your comment. – Tamara Wijsman Nov 16 '12 at 01:19
-
@DaveLucre Determining whether a user can or can not is no issue its the implantation of it. – David Nov 16 '12 at 01:19
-
@TomWijsman Only if you didn't really read my comment. sorry. – gdoron Nov 16 '12 at 01:21
-
1This question is entirely too vague to be answered. Please edit it to clarify exactly what it is you're asking. It reads like a Windows administration/configuration question (user rights for folders) and not a programming question the way it's phrased now. Thanks. – Ken White Nov 16 '12 at 01:22
-
@gdoron: Locked files can't be deleted. sorry. – Tamara Wijsman Nov 16 '12 at 01:23
-
Do you need to prevent the user from being able to delete the file both inside your application, and also using Windows Explorer? If so, can you use NTFS permissions or the Read Only file attributes? – Dave Lucre Nov 16 '12 at 01:24
-
@DaveLucre Actually Read Only is probably the perfect answer, my app wont take their altered file even if they remove read-only and overwrite it then my app would just rewrite to the original file. Post this as an answer, if no one else comes up with something better I'll use yours – David Nov 16 '12 at 01:33
-
@David answer submitted. – Dave Lucre Nov 16 '12 at 01:39
2 Answers
I can think of couple of solutions:
Have your application keep the file opened as read only. This may cause some issues if the number of files grows large, is you will likely hit some ceilings, either in memory, or handle limits.
See this excellent technet blog for some information on handle limits http://blogs.technet.com/b/markrussinovich/archive/2009/09/29/3283844.aspx
Use NTFS permissions. If you run your application as another user, take ownership of the file and allow other users read only access, it should help. Since you are in a business environment you may have that level of control of running applications as services or on startup.
A nice SO post on NTFS permissions Setting NTFS permissions in C#.NET

- 1
- 1

- 466
- 4
- 4
-
Some issues I have: I don't think I would like to use the first for the reasons you mentioned, the second would require it run as admin or service which isn't optimal, and it wouldn't only be on company computers so AD solutions are off the table. On second thought I could run it as a service, but mocking with NTFS permissions is something I would like to avoid. – David Nov 16 '12 at 01:29
-
I'm afraid I don't think you have a lot of options. You want to lock a file, that sounds like something a file system is ideally used to solve. Avoiding NTFS permissions may be something you might want to consider. Also, it would need to run as an Admin user, just a user with sufficient permssions to work with that particular folder. – Phil Martin Nov 16 '12 at 02:23
-
I meant "reconsider". Sorry if there was confusion. I sure hope you find a solution that works for you, it sound like quite the fun application to write. – Phil Martin Nov 16 '12 at 03:13
-
I really like your answer but It's probably not the best way for me to go this time, I did up vote though. – David Nov 16 '12 at 13:27
I suggest you set the Read Only Attribute using the File.SetAttributes method.
To Set the file as Read Only:
File.SetAttributes(@"C:\Users\Owner\Desktop\file.txt", FileAttributes.ReadOnly);
To Set the file back to normal:
File.SetAttributes(@"C:\Users\Owner\Desktop\file.txt", FileAttributes.Normal);
You can check to see if the file is read only, and then throw an error :
System.IO.FileInfo fileInfo = new System.IO.FileInfo(@"C:\Users\Owner\Desktop\file.txt");
if (fileInfo.IsReadOnly)
{
//...Alert user that this file cannot be deleted
}
else
{
//... Delete the file here
}

- 1,105
- 1
- 14
- 16