StrataFrame Forum

Override NTFS permissions in code

http://forum.strataframe.net/Topic20374.aspx

By Keith Chisarik - 10/27/2008

Question:

If I have an application I wrote that executes under Windows login "generaluser", I now want my application in specific scenarios to write a file to a network folder to which "generaluser" does not have write NTFS permissions. How can I accomplish this?

By Trent L. Taylor - 10/28/2008

You can go at this several ways and it can get somewhat complicated depending on the route that you take.  Let's first look at spawning an EXE to perform a certain action with different credentials. 

Dim startInfo As New System.Diagnostics.ProcessStartInfo("C:\MyApp.exe")
Dim password As New System.Security.SecureString()
Dim process As System.Diagnostics.Process

'-- Build the secure password string
For Each c As Char In "MyPassword"
    password.AppendChar(c)
Next

'-- Set the security login credentials
startInfo.UserName = "MyWindowsUser"
startInfo.Domain = "MYDOMAIN"
startInfo.Password = password

'-- Spawn the process
process = System.Diagnostics.Process.Start(startInfo)

'-- If you want to wait for the process to exit you can wait like this
process.WaitForExit()

Now if you want to create isolation levels within your assembly, it gets a lot more complicated.  You will have to apply security attributes and configure each client machine that will execute the code.  I am obviously not getting too deep here as this can become a really big tutorial in and of itself, but if you have this need, then a web search would probably be your best best as I ams ure that there are probably already some articles out on CodeProject or something along those lines showing how to do this.

By Keith Chisarik - 10/28/2008

thanks, all I was hoping for was a namespace or starting place from someone who has done this, Google turned up a lot of options. I'll check out what you posted, appreciated as always.