0

I have a simple c# console application that i am running through a Powershell ps1 file. I would like to take the argument passed in through $args[0] when the script is called, and use that in a method i have in my c# application. I have found a lot for the reverse, but nothing to helpful for what i am trying to do. Any input would be much appreciated. This is roughly what i have tried recently. Thank you!

$filePath = $args[0]
$Check = New-Object -ComObject "Take c# file and try to make into    object"
$Check.hasFile($filePath)
Start-Process -FilePath "Path to c# exe"

.hasFile is the c# method that i am trying to pass the Powershell argument to.

1 Answer 1

1

If I'm understanding you correctly, you just want to pass your script variable as an argument into your C# application? If so, you can just make the Main method of your Program.cs accept an array of strings, and then pass your script variables to Start-Process when you launch the application:

$myargs = @($args[0],$myvariable, .... <whatever>)
Start-Process MyCSharpApplication.exe $myargs

Hope this helps!

EDIT:

To clarify, in the simple case that you just have one variable you want to pass in, you don't even need the separate array variable. You can just do:

Start-Process MyCSharpApplication.exe $myvariable
Sign up to request clarification or add additional context in comments.

4 Comments

Yes that is exactly what i want to do! So in your example, would <whatever> be what i would call the array in program.cs so the variable could be used in program.cs?
The normal convention is to use the following method signature: static Main(string[] args) (which is the method stub you should have had when you created the project, no?). So then if you're calling your C# application from your .ps1 script with one argument, you would just access args[0] inside your Main method
I cannot tell you how long i've been trying to figure out that simple line of code. Thank you, I really appreciate your help!!
No problem at all, glad to have been of assistance!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.