I am writing a small program to copy the files from one server to another and for this I am using xcopy
command from the C# code. I want to execute the process as different user for which I am using the following code -
string sourceLoc = @"c:\test\xyz.xlsx";
string destinationLoc = @"c:\subfolder";
var abc= "Password";
var pass = new System.Security.SecureString();
foreach (char c in abc)
{
pass.AppendChar(c);
}
// Use ProcessStartInfo class
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.UserName = "admin";
startInfo.Password = pass;
startInfo.Domain = "domain";
startInfo.Verb = "runas";
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "xcopy";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "\"" + sourceLoc + "\"" + " " + "\"" + destinationLoc + "\"" + @" /e /y /I";
try
{
using (Process exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
}
catch (Exception exp)
{
throw exp;
}
But i am getting the following error :-
Unhandled Exception: System.ComponentModel.Win32Exception: The directory name is invalid at LogReader.Program.Main(String[] args) in C:\Users\sdg\documents\visual studio 2010\Projects\LogReader\LogReader\Program.cs:line 66
If I run the program without providing the other user credentials it works fine.