简体   繁体   中英

Directory.GetCurrentDirectory

I've written a program that checks if it is in a specific folder; if not, it copies itself into that folder,run the copied program and exit. but the problem is when I call Directory.GetCurrentDirectory(); in the copied program(only when It runs by the first one) I get the directory of the first program not the copied one. What's the problem here?

the code:

if(Directory.GetCurrentDirectory()!=dir)
{
      File.Copy(Application.ExecutablePath,dir+name);
      System.Diagnostics.Process.Start(dir+@"\"+name);
      System.Environment.Exit(System.Environment.ExitCode);
}

i summarized my codes.

You have to use the WorkingDirectory on the processinfo, no need for copying the files.

if(Directory.GetCurrentDirectory()!=dir)
{
  string exepath = Path.Combine(dir,name);
  ProcessStartInfo processStartInfo = new ProcessStartInfo();
  process.StartInfo.FileName = exepath;
  processStartInfo.WorkingDirectory = dir;

  //Set your other process info properties

  Process process = Process.Start(processStartInfo);
  System.Environment.Exit(System.Environment.ExitCode);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM