C
C#•3y ago
justaeris

Check for required admin rights to edit a file [Answered]

Heyo! In short I use SDelete to shred files and I want to know before running the program if any file requires admin rights to be deleted. Is that possible ?
51 Replies
Instinct
Instinct•3y ago
With C#? Otherwise check for Delete permission in Properties -> Security -> Advanced
justaeris
justaerisOP•3y ago
yeah with C# 😅 ma men instinct commin' clutch again goal is to ask the user for admin rights before to avoid files being left behind after operation
Cisien
Cisien•3y ago
you have to read the file's permissions and see if the user running the command is in the acl or not
justaeris
justaerisOP•3y ago
how do you get the perms ? and what does acl means ?
Cisien
Cisien•3y ago
access control list the ntfs permissions
justaeris
justaerisOP•3y ago
oh okay huuuh how do you get access to that in c# I see nothing useful in FileInfo
Cisien
Cisien•3y ago
File.GetSecurityContext or something like that checking... something like this: File.GetAccessControl(@"c:\somefile.txt").GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)); file permissions are quite complex
justaeris
justaerisOP•3y ago
I don(y have .GetAccessControl
Instinct
Instinct•3y ago
GetAttributes maybe?
justaeris
justaerisOP•3y ago
not that
Cisien
Cisien•3y ago
that's in framework core may be different
justaeris
justaerisOP•3y ago
FileSecurity
Cisien
Cisien•3y ago
new FileInfo("").GetAccessControl().GetAccessRules(...) or that GetAccessControl is an extension method
justaeris
justaerisOP•3y ago
var fs = new FileSecurity(info.FullName, AccessControlSections.All);
fs.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
var fs = new FileSecurity(info.FullName, AccessControlSections.All);
fs.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
this ?
Cisien
Cisien•3y ago
that's checking just owner there's more to it for file access you want all sections
justaeris
justaerisOP•3y ago
edited is that what I want to check for write acess ? bro this looks complex as shit who decided it'd be a good idea to do this
Cisien
Cisien•3y ago
that's part of it that should get you all of the access rules but yeah, it's very complex you have to take them in whole as one rule can invalidate previous
justaeris
justaerisOP•3y ago
can't I just fiond a workaround involving trying to write to the file and catchiçng an exception ?
Cisien
Cisien•3y ago
you can do that
justaeris
justaerisOP•3y ago
how'd that work ?
Cisien
Cisien•3y ago
that's honestly how most filesystem access problems get solved
justaeris
justaerisOP•3y ago
would it throw if I open a file I don't have access to ?
Cisien
Cisien•3y ago
try { something to do with teh filesystem } catch (whatever exception it throws) { report Needs elevated access to user}
justaeris
justaerisOP•3y ago
so wait
Cisien
Cisien•3y ago
if you want read access, yeah
justaeris
justaerisOP•3y ago
just to be sure
Cisien
Cisien•3y ago
if you want to delete the file, that may be different
justaeris
justaerisOP•3y ago
a user can have write access to a directory but no to the files in the dir right ? I think I need write access
Cisien
Cisien•3y ago
File.OpenWrite might throw
justaeris
justaerisOP•3y ago
lemme test let's go babyyyyyyyyyyy
justaeris
justaerisOP•3y ago
cisien is writing this await File.OpenWrite(info.FullName).DisposeAsync(); legal ? like directly .Disposing no what is this garbage you can't set UseSHellExecute to true if you redirect output streams but you also can't run as admin if you dan"t have SheleExecute to true
Cisien
Cisien•3y ago
probably though you won't call DisposeAsync if OpenWrite throws but you may not need to. i'm not sure what the side effects are if opening fails.
jcotton42
jcotton42•3y ago
@Aeris this sounds like you're just setting yourself up for a TOCTOU bug just delete the file, and handle errors if applicable sdelete probably returns a specific error code on access denied
justaeris
justaerisOP•3y ago
just throws would work if I were shredding files one by one
Cisien
Cisien•3y ago
what i'm not sure if is if there are native resources held after the throw
justaeris
justaerisOP•3y ago
but I just chuck everything into a single command
justaeris
justaerisOP•3y ago
Cisien
Cisien•3y ago
keep in mind this code will be slow
justaeris
justaerisOP•3y ago
why ?
Cisien
Cisien•3y ago
you're doing file i/o to see if it succeeds, then performing logic based on exceptions, which tend to be slow
justaeris
justaerisOP•3y ago
oh welp
Cisien
Cisien•3y ago
if you're doing it frequently (like that) you can expect to see this take some time if there are a large number of files
justaeris
justaerisOP•3y ago
it seemed fine from the few runs I did other way around is to run sdelete file per file
Cisien
Cisien•3y ago
test it, it may not be a problem for your scenario
justaeris
justaerisOP•3y ago
and as jcotton said see if it returns a specific exit code how many files is "a large number of files"? returns 0
Cisien
Cisien•3y ago
whatever number doesn't cause issues 🙂
justaeris
justaerisOP•3y ago
wdym ?
Cisien
Cisien•3y ago
the actual number doesn't matter, it's going to vary per system could be 50, could be 50,000
justaeris
justaerisOP•3y ago
oh yeah true welp guess I'm gonna stuck to my solution alright thanks yall for your help, really appreciated have a good day °D°
Accord
Accord•3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server