C
C#•2y 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•2y ago
With C#? Otherwise check for Delete permission in Properties -> Security -> Advanced
justaeris
justaeris•2y 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•2y ago
you have to read the file's permissions and see if the user running the command is in the acl or not
justaeris
justaeris•2y ago
how do you get the perms ? and what does acl means ?
Cisien
Cisien•2y ago
access control list the ntfs permissions
justaeris
justaeris•2y ago
oh okay huuuh how do you get access to that in c# I see nothing useful in FileInfo
Cisien
Cisien•2y 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
justaeris•2y ago
I don(y have .GetAccessControl
Instinct
Instinct•2y ago
GetAttributes maybe?
justaeris
justaeris•2y ago
not that
Cisien
Cisien•2y ago
that's in framework core may be different
justaeris
justaeris•2y ago
FileSecurity
Cisien
Cisien•2y ago
new FileInfo("").GetAccessControl().GetAccessRules(...) or that GetAccessControl is an extension method
justaeris
justaeris•2y 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•2y ago
that's checking just owner there's more to it for file access you want all sections
justaeris
justaeris•2y 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•2y 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
justaeris•2y ago
can't I just fiond a workaround involving trying to write to the file and catchiçng an exception ?
Cisien
Cisien•2y ago
you can do that
justaeris
justaeris•2y ago
how'd that work ?
Cisien
Cisien•2y ago
that's honestly how most filesystem access problems get solved
justaeris
justaeris•2y ago
would it throw if I open a file I don't have access to ?
Cisien
Cisien•2y ago
try { something to do with teh filesystem } catch (whatever exception it throws) { report Needs elevated access to user}
justaeris
justaeris•2y ago
so wait
Cisien
Cisien•2y ago
if you want read access, yeah
justaeris
justaeris•2y ago
just to be sure
Cisien
Cisien•2y ago
if you want to delete the file, that may be different
justaeris
justaeris•2y 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•2y ago
File.OpenWrite might throw
justaeris
justaeris•2y ago
lemme test let's go babyyyyyyyyyyy
justaeris
justaeris•2y 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•2y 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•2y 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
justaeris•2y ago
just throws would work if I were shredding files one by one
Cisien
Cisien•2y ago
what i'm not sure if is if there are native resources held after the throw
justaeris
justaeris•2y ago
but I just chuck everything into a single command
Cisien
Cisien•2y ago
keep in mind this code will be slow
justaeris
justaeris•2y ago
why ?
Cisien
Cisien•2y ago
you're doing file i/o to see if it succeeds, then performing logic based on exceptions, which tend to be slow
justaeris
justaeris•2y ago
oh welp
Cisien
Cisien•2y 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
justaeris•2y ago
it seemed fine from the few runs I did other way around is to run sdelete file per file
Cisien
Cisien•2y ago
test it, it may not be a problem for your scenario
justaeris
justaeris•2y 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•2y ago
whatever number doesn't cause issues 🙂
justaeris
justaeris•2y ago
wdym ?
Cisien
Cisien•2y ago
the actual number doesn't matter, it's going to vary per system could be 50, could be 50,000
justaeris
justaeris•2y 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•2y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server
More Posts