0

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.

TheEvilPenguin
  • 5,634
  • 1
  • 26
  • 47
Durgesh Sahu
  • 247
  • 1
  • 5
  • 19
  • Is the user from which you are launching it an admin? Because you can't run a program from C# as admin without being an admin (imagine the security holes in that) – Icemanind Feb 06 '15 at 02:03
  • @icemanind , thanks for replying, No its not an admin, is this causing the issue because i have used impersonate user to get the running service status using the same user credentials. – Durgesh Sahu Feb 06 '15 at 02:06

1 Answers1

1

I haven't replicated your environment, so this is just an idea, but I've been able to replicate this error by setting ProcessStartInfo.WorkingDirectory to a folder the current user doesn't have access to. If it defaults to the current applications working folder, it could be trying to use a path under c:\Users\ which the other user doesn't have access to.

TheEvilPenguin
  • 5,634
  • 1
  • 26
  • 47
  • thanks for the reply bro, yes you are right the issue was with the permission on current application working directory, i solved it by setting the ProcessStartInfo.WorkingDirectory value to another folder. – Durgesh Sahu Feb 06 '15 at 02:26