File permission - c#

If by some program fault a file is locked, is there any way to remove that lock.
please explain how file unlocking software works. Looking forward for ideas

Microsoft offers a program called Process Explorer which can display help you figure out which program has a particular file or directory open. This may be the solution to the problem you're talking about, if some process that you're unaware of is accessing the file and preventing other access to it.

Related

C# - Locking folders from being edited

i'm making a small project. it's Windows Form Application. i got some sources in a folder (C:/sources). When my program runs, it uses sources from the folder. Currently i can edit the folder by windows explorer, it can cause errors for my program. So i want to lock the folder (C:/sources) from being edited/renamed/deleted when my program runs. How to do so?
EDIT;
Is it possible to show a message like this when user has tried to edit the folder:
"the action cannot be completed because the folder or a file in it is open in another program"
the program that we are talking about is mine..
There are a couple of approaches that you could venture and they vary in difficulty of implementation. It all depends on how important this task is for you. But, before discussing these options; can't you embed those resources in your WinForms application instead? If this is not an option then you can do one of the following:
Write a device driver that can prohibit the access of such resources if your application is running. There are fallbacks to this approach. For example one can impersonate your application by having the same name. But, am not getting in to too much details in trying to break any approach as I am trying to address possible solutions to the current problem. There are different types of drivers that you can consider. Probably the simplest form of this approach would be to implement a mini-filter driver.
Hook certain API's like CreateFile(), NtCreateFile(), ZwCreateFile() although there are many ways to circumvent such mechanism of defense. But, again we are only venturing what you can do to address this constraint of yours.
Open these resources directly from your application and lock it exclusively. For example:
File.Open("test.txt", FileMode.Open, FileAccess.Read, FileShare.None);
as this will result in people getting the message that you desire if they try to open the file.
Maybe you can give more information on what these resources are and we can help you determine which is the best way to protect your files in a reasonable fashion?
Although I don't believe it's the best idea to have files that are critical to the application in a open area like the C: drive, I would look into NTFS file permissions and set the folder to read only, but this wont stop administrative users
See these two posts
restrict access to folder outside of program c#
Setting NTFS permissions in C#.NET

free a file used by others process in c#

I would like in a software to log:
which processes are accessing a file
and
how to force free file from access by others processes (is it possible with API window?)
notice that you are enable to open a file with notepad and delete this file in disk without kill notepad.but for other program like word excel it is not possible. so there some program that lock resource other no.It means you can free but how? secondly it is possible that for some process they crash when you free but in my case it was only custom services that access those files
Thank you
Please look at
http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/37dc9843-2583-41fc-8260-b78183aa4bed/
This might be of some help.

How to access a file that is being used by another process in Windows?

I see that this question has been asked many times in stackoverflow, as well as in other places. The questioners solved their problem by different methods. Most of these solutions are around how they designed their applications and locking level on the files.
I understand that Windows provides Read/Write protection in different ways. But, still, if the file is on my disk, and being an administrator, can't I read the contents of the locked file forcefully? Any pointers on how I can achieve that would be greatly helpful.
The file in question is totally inaccessible to other processes through C# .NET APIs. Looks like its readlocked.
Thanks.
If file is locked from your app, you have to close all buffers/streams or whatever you have opened before.
If file is locked from something else the only solution I know is to use Unlocker, which can be run even without GUI using some console param (so using Process class in C#).
But be careful to walk this way, because you could corrupt file and create some memory leak closing with brute force opened handles and probably application that was using them.

Intermittent "File in Use" Error

I have some code that I wrote to basically clear out the directory every time the program runs through this point. I didn't want to bother enumerating files. If this is a bad way to do this, please tell me.
My main question, however, is about how to deal with the following: one of the files in the folder appears to be in use when it is most certainly not. The program runs on a ButtonClick event, and it exploded the first four or five times, but it worked after I confirmed that nobody was using the file on the server. There is only one person besides myself that would have been using it, and he confirmed that there was nothing running on his side that would be touching the file. Any ideas for what would cause this error/how to avoid it/how to handle it?
I am also having trouble reproducing the error...
string directory = #"\\server\directory\folder\";
DirectoryInfo di = new DirectoryInfo(directory);
if (di.Exists)
di.Delete(true);
Directory.CreateDirectory(directory);
If you are using Windows XP, this may help : http://msdn.microsoft.com/en-us/library/dd997370.aspx#remove_open_handles
Just an extract from the top of this page :
"If you are running Windows XP or earlier, a delete operation on a file or directory that follows an enumeration could fail if there is an open handle that remains on one of the enumerated directories or files."
You may also use a software like Unlocker to identify the process locking your file.
If the file is in use, then someone is most certainly using it. :)
If you can access the server the files reside on, you can use a tool such as Process Explorer to find out which process has opened the file.

c# Programme Error

Unable to copy file "obj\Debug\FootballLeague.exe" to "bin\Debug\FootballLeague.exe". The process cannot access the file 'bin\Debug\FootballLeague.exe' because it is being used by another process.
I got this problem and I couldnt find any another process.Other c# programmes are working properly.I changed the place which I haved saved first time, but I couldnt get any clue to find the error.
Chances are the other process is FootballLeague.exe - are you sure you aren't still running it?
Another alternative is to use Process Explorer to find out what's got a handle on the file.
If your FootballLeague.exe has been launched and is still running, it is the process that is locking the file.
Do you have a virus scanner turned on? They tend to open files for exclusive access at just the wrong time. I would advise that you turn off real-time scanning in your project directory at a minimum.
Other than that, you'll want to use something like Process Explorer to find out who actually has the file open.

Categories