0

The scenario is to start elevated cmd which calls a bat file and right after starting the process the app quits. The code from the wpf app:

private static void CmdCommand(string filename, string workingDir)
{
  var processInfo = new ProcessStartInfo(workingDir + filename);
  processInfo.UseShellExecute = true;
  processInfo.Verb = "runas";
  processInfo.WorkingDirectory = workingDir;
  processInfo.Arguments = "\""+workingDir+"\"" ;
  Thread t1 = new Thread(new ThreadStart(
    delegate
    {
      Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
      {
        Process.Start(processInfo);
      }));
    }));
  t1.Start();
  Thread t2 = new Thread(new ThreadStart(
    delegate
    {
      Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
      {
        System.Windows.Application.Current.Shutdown();
      }));
    }));
  t2.Start();
}

The code in the bat file:

set Path1=%~1
ping 127.0.0.1 -n 6 > nul
copy /Y "%Path1%dbg\lib.dll" "%Path1%lib.dll"
pause
ping 127.0.0.1 -n 6 > nul
call "%Path1%wpf.exe"

The result is that the cmd appears for a moment and disappears and no result. This works when not elevated. But I need it to be elevated as there are no rights to copy in the working dir. I tried recording screen to pause when the cmd appears but the recorder scips this part. So how can I debbug it?

EDIT

Actually it is working but without one line

processInfo.Arguments = "\""+workingDir+"\"" ;

and I need to pass the arguments

EDIT 2

Apparently the problem is """ but if I don't add those I'll have a bug with paths containing spaces.

Daniel Filipov
  • 325
  • 4
  • 17
  • This one might help you: https://stackoverflow.com/questions/8167610/how-do-i-attach-visual-studio-to-a-process-that-is-not-started-yet – nilsK Jan 20 '23 at 14:07
  • 1
    Did you look in event viewer? Have you considered doing this copying using csharp in the app instead? You could force the app to require admin privileges using the manifest. – Andy Jan 20 '23 at 14:07
  • You checked the user has admin rights on this pc? – Andy Jan 20 '23 at 14:08
  • I want to avoid elevating app itself and the app is using the file copied so I can't do it from the app. – Daniel Filipov Jan 20 '23 at 14:09
  • I don't understand why, if you're using a high level programming language, you need to run a batch file to simply copy a file, and run an executable file, (with a delay in between)! Please explain what you mean by "the app is using the file". – Compo Jan 20 '23 at 16:32
  • Additionally, why are you creating a variable, 1. ```@Copy /Y "%~1dbg\lib.dll" "%~1"```, 2. ```@%SystemRoot%\System32\timeout.exe /T 5 /NoBreak 1>NUL```, 3. ```@Start "" "%~1wpf.exe"```. – Compo Jan 20 '23 at 16:40
  • @Compo The app loads the lib and uses functionality with import. – Daniel Filipov Jan 25 '23 at 07:39

0 Answers0