RichardKline
asked on
Create New File in share using alternate user credentials
I am writing an applicaion which will create a text file in a networked share which the currently logged in user may not have proper permissions. I've written a console app to model the process.
My thought was to first programmatically open the share using the alternate credentials and then create the file.
Using the model code below, I receive an "The parameter is incorrect" error at p.start()
Research indicates that I may be stuck without shelling out to a "Net Use" statement. Reasons:
1. Specifying User requires: UseShellExecute = False
2. UseShellExecute = False requires: an EXE file be defined as the FileName
The share does exist and the code works if I don't specify the domain, user and password parameters but....
I would prefer to think that there's another way which I'm missing. Any thoughts?
Thank you.
The model code reads:
' Define Path and name for Text File
Dim TextFilePath As String = "\\ServerName\ShareName"
Dim TextFileName As String = "LogFile.Txt"
' Define Password SecureString
Dim s As String = "PaSsWoRd"
Dim ss As New System.Security.SecureStri ng
Dim c As Char() = s.ToCharArray
For i As Integer = 0 To s.Length - 1
ss.AppendChar(c(i))
Next
' Attempt to Open Path (Network share) using alternate credentials
Dim p As Process = New Process
With p.StartInfo
.Domain = "DomainName"
.UserName = "UserName"
.Password = ss
.FileName = TextFilePath
.UseShellExecute = False
End With
Try
p.Start()
Console.WriteLine("Path is OK")
' Path is open. Now Create and Write to File.
Try
Dim oWrite As System.IO.StreamWriter
oWrite = IO.File.CreateText(TextFil ePath & "\" & TextFileName)
Console.WriteLine("Can open File")
Try
oWrite.WriteLine("Success" )
oWrite.Flush()
oWrite.Close()
oWrite.Dispose()
Console.WriteLine("Can write to File")
Catch ex As Exception
' Unable to Write to File
Console.WriteLine(ex.Messa ge)
End Try
Catch ex As Exception
' Unable to Create File
Console.WriteLine(ex.Messa ge)
End Try
Catch ex As Exception
' Unable to Open Share
Console.WriteLine(ex.Messa ge)
End Try
My thought was to first programmatically open the share using the alternate credentials and then create the file.
Using the model code below, I receive an "The parameter is incorrect" error at p.start()
Research indicates that I may be stuck without shelling out to a "Net Use" statement. Reasons:
1. Specifying User requires: UseShellExecute = False
2. UseShellExecute = False requires: an EXE file be defined as the FileName
The share does exist and the code works if I don't specify the domain, user and password parameters but....
I would prefer to think that there's another way which I'm missing. Any thoughts?
Thank you.
The model code reads:
' Define Path and name for Text File
Dim TextFilePath As String = "\\ServerName\ShareName"
Dim TextFileName As String = "LogFile.Txt"
' Define Password SecureString
Dim s As String = "PaSsWoRd"
Dim ss As New System.Security.SecureStri
Dim c As Char() = s.ToCharArray
For i As Integer = 0 To s.Length - 1
ss.AppendChar(c(i))
Next
' Attempt to Open Path (Network share) using alternate credentials
Dim p As Process = New Process
With p.StartInfo
.Domain = "DomainName"
.UserName = "UserName"
.Password = ss
.FileName = TextFilePath
.UseShellExecute = False
End With
Try
p.Start()
Console.WriteLine("Path is OK")
' Path is open. Now Create and Write to File.
Try
Dim oWrite As System.IO.StreamWriter
oWrite = IO.File.CreateText(TextFil
Console.WriteLine("Can open File")
Try
oWrite.WriteLine("Success"
oWrite.Flush()
oWrite.Close()
oWrite.Dispose()
Console.WriteLine("Can write to File")
Catch ex As Exception
' Unable to Write to File
Console.WriteLine(ex.Messa
End Try
Catch ex As Exception
' Unable to Create File
Console.WriteLine(ex.Messa
End Try
Catch ex As Exception
' Unable to Open Share
Console.WriteLine(ex.Messa
End Try
ASKER
Thank you.
VB.NET 2005 (2.0 .Net Framework).
The desired end result is to write content to a file in the share regardless of the logged in user's assigned permissions.
VB.NET 2005 (2.0 .Net Framework).
The desired end result is to write content to a file in the share regardless of the logged in user's assigned permissions.
Can you use impersonation for the entire process?
Bob
Bob
ASKER
If that is what needs to be done. By these questions, do you believe that there isn't an application only solution?
No, I don't because while you are opening the share with the alternate credentials, you are writing the file with the current credentials.
Bob
Bob
ASKER
OK,
Is it then possible to programmatically open and write to a text file on the network share using alternate credentials?
The code above was my attempt to get that end result. I would like to obtain that end result using some code but not necessarily the lines above.
Thank you
Is it then possible to programmatically open and write to a text file on the network share using alternate credentials?
The code above was my attempt to get that end result. I would like to obtain that end result using some code but not necessarily the lines above.
Thank you
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Bob