0

This is my code working in powershell, but to put it in C# I must replace "$string" to "$string2"; but it's not working (: it must be "$string" to work... I tried '$string' and ($string) and a lot but just this "$string" working. Help ?

Powershell Code Working 100% :
$string2 = C:/Users/Sycho/Desktop/youtube-dl.exe -F https://youtu.be/f1A7SdVTlok | Out-String -stream | Select-String 133 ; "$string2".SubString(105)
     var mainForm = Application.OpenForms.OfType<Form1>().Single();
     string urlBoxText3 = mainForm.Controls["URL_BOX"].Text;
     ProcessStartInfo startInfo6 = new ProcessStartInfo();
     startInfo6.FileName = @"powershell.exe";
     startInfo6.Arguments = "$string2 = C:/Users/Sycho/Desktop/youtube-dl.exe -F " + urlBoxText3 + "| Out-String -stream | Select-String 133 ; \"$string2\".SubString(105)";
     startInfo6.RedirectStandardOutput = true;
     startInfo6.RedirectStandardError = true;
     startInfo6.UseShellExecute = false;
     startInfo6.CreateNoWindow = true;
     startInfo6.Verb = "runas";
     Process process6 = new Process();
     process6.StartInfo = startInfo6;
     process6.Start();
     string output = process6.StandardOutput.ReadToEnd();
     label4.Text = output;

Sycho
  • 23
  • 1
  • 5
  • 1
    Show us your C# code. – Robert Harvey Feb 25 '22 at 12:58
  • 1
    Please include your PowerShell code as text, not as an image – stuartd Feb 25 '22 at 13:01
  • @RobertHarvey a program to download youtube videos using youtube-dl....i shared all you need to know to help me. – Sycho Feb 25 '22 at 13:01
  • Why are you using PowerShell just to run an exe file from C#, rather than just running it directly with the new process? – stuartd Feb 25 '22 at 13:02
  • @stuartd I just want to get the video size...so I can't do it with the exe I am doing it with powershell and filtering output – Sycho Feb 25 '22 at 13:04
  • 2
    What I mean is, you can replicate the successful powershell call by setting the process file name to `C:/Users/Sycho/Desktop/youtube-dl.exe` and the arguments to `"-F " + urlBoxText3`. Then you can use C#'s string methods to extract the part you want from the output. – stuartd Feb 25 '22 at 13:12
  • @stuartd see this plz https://i.stack.imgur.com/pUmXV.png .......... can I show MB only ?with C#'s string methods ? – Sycho Feb 25 '22 at 13:20
  • 1
    Why are you executing `powershell.exe` anyway? You can execute a Powershell script directly using [`System.Management.Automation.Powershell`](https://learn.microsoft.com/en-us/powershell/scripting/developer/hosting/adding-and-invoking-commands?view=powershell-7.2) – Charlieface Feb 25 '22 at 13:21
  • _can I show MB only_ - sure. But it's not possible to tell you _how_ from just an image. – stuartd Feb 25 '22 at 13:50

1 Answers1

2
  • You say that PowerShell must ultimately see "$string2".SubString(105) for your command to work.

  • When you call PowerShell via its CLI, powershell.exe, and pass commands to it via the (positionally implied) -Command / -c parameter, you must escape " characters as \" in order for PowerShell to retain them as part of the command(s) to execute after command-line parsing.

Therefore, replace \"$string2\" with \\\"$string2\\\" (sic).


Taking a step back: You could streamline your code as follows, which obviates the need for enclosing $string2 in "..."

startInfo6.Arguments = "$string2 = (C:/Users/Sycho/Desktop/youtube-dl.exe -F " + urlBoxText3 + " | Select-String 133).Line; $string2.SubString(105)";
  • By accessing the .Line property on the output object emitted by the Select-String call, a string - the matching line - is directly returned (rather than the match-info object that is emitted by default).

  • Out-String -Stream was removed, because it is unnecessary: PowerShell exhibits this behavior by default when calling external programs such as youtube-dl.exe.

mklement0
  • 382,024
  • 64
  • 607
  • 775