• 0

C# File Access Problems


Question

I am working on a project that reads and writes data to a couple files. Now, this works properly until it it begins being used by multiple users. Due to the way the program works, it need to be able to read and modify the files regardless of the user.

The Setup:

Two token files (License files) Stored in:

C:/ProgramData/MyCompany/MyProject/myToken.tkn

C:/ProgramData/Temp/1234jklqsdjlkqhsd== -> Same as the Token, just the file name/extension encrypted

1 Debug Log

C:/ProgramData/MyCompany/MyProject/logs/debug.log

How the program works.

1. The program is run by an initial user. It is then activated with a number of uses license which is saved into the token file, then copied into the temp path with the name encrypted. The debug.log file is accessed whenever something needs to be written. The files are accessed using a streamreader / streamwriter object.

2. The program is then run by another user on the same computer, the software runs and the number of uses decreases and in turn updates the primary token, removes the temp token, then copies the modifed token over. All progress is logged in the log.

What's happening

The primary user gets no access problems, can created tokens, and debug log no issues. When the next user logs in the program can read from the tokens and the logs, but cannot write to them. Looking at the permissions of the files, the user is not set to "modify" access, only Administrators and the Initial Creator can. Now I cannot have this program be run as administrator due to security reasons. Also, the files cannot be on a per-user basis as the tokens must be in a shared directory so all uses can update the tokens.

I've tried to change the permissions on the files to grant all people in the "Users" group read, write, execute. This worked until the next user logged in and wrote to the file, this resulted in the permissions being reset on the file and stopped the allowance of other users to modify the file, and also set the file as them being the owner.

I have seen that you can run a program called icacls however it required Admin to be run.

What I need to know

Is there a way to set file permissions for a file on create/modify so that I can ensure that the file can be accessed and changed by all regardless? I've looked online and I have only seen one option that isn't viable, and that is to create a "File Creator / Modifier" account, and have the program pretend to be that user when modifying the files. As the users are domain controlled, and is part of a very tightly controlled network, computer accounts cannot be created.

Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0

I was doing something "similar" to this over the last couple of days, but it's more just copying a token file to multiple directories. This may help you though. Setting the file permissions isn't hard at all:

http://stackoverflow.com/questions/7590446/set-file-permissions-in-c-sharp

Link to comment
Share on other sites

  • 0

I may not fully understand what you are trying to achieve, so if I miss the mark I apologize.

The idea as I understand it is that you have a file (or even multiple files) that one or more users can access and each and every user should be able to make and save changes to a file, even if another user currently has that same file open. I would user the DirectoryWatcher to monitor the file that the user has "opened", if the file is modifeied (change event) then the new changes would be loaded into any other user who has that file open. As changes are made (every X characters or Y time) those changes are written back to the file. Open, make changes and then close the file. This way only one user has it open at a moment. If the file is open by user "A" when user "B" makes a changes, a small loop occurs to gather the new changes (and integrate into user "B"'s document before the changes are made by user "B"

I hope this helps, or at least gives you a laugh. Multiple users into a single document are an issue with collision

Link to comment
Share on other sites

  • 0

I was doing something "similar" to this over the last couple of days, but it's more just copying a token file to multiple directories. This may help you though. Setting the file permissions isn't hard at all:

http://stackoverflow...ions-in-c-sharp

Hmm, thanks I'll look into this.

I may not fully understand what you are trying to achieve, so if I miss the mark I apologize.

The idea as I understand it is that you have a file (or even multiple files) that one or more users can access and each and every user should be able to make and save changes to a file, even if another user currently has that same file open. I would user the DirectoryWatcher to monitor the file that the user has "opened", if the file is modifeied (change event) then the new changes would be loaded into any other user who has that file open. As changes are made (every X characters or Y time) those changes are written back to the file. Open, make changes and then close the file. This way only one user has it open at a moment. If the file is open by user "A" when user "B" makes a changes, a small loop occurs to gather the new changes (and integrate into user "B"'s document before the changes are made by user "B"

I hope this helps, or at least gives you a laugh. Multiple users into a single document are an issue with collision

Not Quite. You started off correctly, I have multiple files that need to be able to be modified by multiple users. Then you got wrong. Windows by default only gives write/modify permissions to the owner (who made it) or Administrators. Other users cannot modify the files. As having the program run as admin isn't a possible scenerio I need a way to change the file permissions to unlock it so everyone inside the user group can modify it. I also need the change in permissions to persist through file modifications.

Link to comment
Share on other sites

  • 0

Not Quite. You started off correctly, I have multiple files that need to be able to be modified by multiple users. Then you got wrong. Windows by default only gives write/modify permissions to the owner (who made it) or Administrators. Other users cannot modify the files. As having the program run as admin isn't a possible scenerio I need a way to change the file permissions to unlock it so everyone inside the user group can modify it. I also need the change in permissions to persist through file modifications.

I'm not saying that an app should run with admin privileges.

Let me try this example: Open Notepad and write something, then open another instance of Notepad. In instance 1 you can go ahead and write something but instance two does not see the changes. Both instances of Notepad can write and then save changes, each time a user saves the "old" data is lost because Notepad is not notified of the changes by the other instance.

What I was attempting to say is to monitor the file using the DirectoryWatcher class so that when changes happen Notepad (in this example) would be notifed of the changes. You would not have to keep the file "locked" by whatever mechanism as the data would only be a "copy" of what is in the file.

I hope that clarifies a little more.

Link to comment
Share on other sites

  • 0

I'm not saying that an app should run with admin privileges.

Let me try this example: Open Notepad and write something, then open another instance of Notepad. In instance 1 you can go ahead and write something but instance two does not see the changes. Both instances of Notepad can write and then save changes, each time a user saves the "old" data is lost because Notepad is not notified of the changes by the other instance.

What I was attempting to say is to monitor the file using the DirectoryWatcher class so that when changes happen Notepad (in this example) would be notifed of the changes. You would not have to keep the file "locked" by whatever mechanism as the data would only be a "copy" of what is in the file.

I hope that clarifies a little more.

Yea, I think you are confused what I am trying to do. I am not trying to open the files concurrently. The users are users on the one computer. They are made by UserA on the computer, UserA logs out, then UserB logs in. Because UserA made the files, UserB cannot modify them. If the program is launched as admin though, the admin has write access because that's how windows rolls.

Link to comment
Share on other sites

  • 0

Yea, I think you are confused what I am trying to do. I am not trying to open the files concurrently. The users are users on the one computer. They are made by UserA on the computer, UserA logs out, then UserB logs in. Because UserA made the files, UserB cannot modify them. If the program is launched as admin though, the admin has write access because that's how windows rolls.

Yea, I was confused. Sorry for wasting your time. I will see if there is anything I can think of to deal with your issue.

Link to comment
Share on other sites

  • 0

Isn't there an 'Everybody' permission you might be able to set on the files?

http://stackoverflow...der-using-c-net

that may work for your files as well.


static void Main(string[] args)
{
using (FileStream FileImWriting = File.Create(args[0], 64, FileOptions.None))
{
// Do w/e you were doing with the file, lalalala.
}

FileSecurity Security = File.GetAccessControl(args[0]);
SecurityIdentifier Everybody = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
Security.AddAccessRule(new FileSystemAccessRule(Everybody, FileSystemRights.FullControl, AccessControlType.Allow));
File.SetAccessControl(args[0], Security);
}
[/CODE]

this is a small example I made to test the functionality and make sure it worked, it appears the file I created does indeed set the 'Everybody' token with Full Access. You could modify this to just give them modify or something similar.

Edited by FuhrerDarqueSyde
Link to comment
Share on other sites

  • 0

Isn't there an 'Everybody' permission you might be able to set on the files?

http://stackoverflow...der-using-c-net

that may work for your files as well.


static void Main(string[] args)
{
using (FileStream FileImWriting = File.Create(args[0], 64, FileOptions.None))
{
// Do w/e you were doing with the file, lalalala.
}

FileSecurity Security = File.GetAccessControl(args[0]);
SecurityIdentifier Everybody = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
Security.AddAccessRule(new FileSystemAccessRule(Everybody, FileSystemRights.FullControl, AccessControlType.Allow));
File.SetAccessControl(args[0], Security);
}
[/CODE]

this is a small example I made to test the functionality and make sure it worked, it appears the file I created does indeed set the 'Everybody' token with Full Access. You could modify this to just give them modify or something similar.

Awesome, that should work I will give it a try. The problem I was facing is that the StreamWriter kept resetting the file permissions on me back to the default settings. I could set the permissions manually after the face, and it would work once.. until the Streamwriter would reset the permissions.

  • Like 1
Link to comment
Share on other sites

  • 0

Glad I could help, let me know if it works for you.

Link to comment
Share on other sites

  • 0

After running the program through some tests it appears to be working exactly as I wanted thanks for the fix Fuhrer!

  • Like 1
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.