문제

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)

도움이 되었습니까?

해결책 2

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
}

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top