1

I am having an issue where the SelectedIndexChanged event is not firing on my System.Windows.Forms.ComboBox object when it should, but only on my computer. It works fine on everyone else's computer.

When I run GetType().assembly on the object my computer shows .Net 4.0, but 2.0 on everyone else's.

How can I force the object to 2.0?

Here is the relevent assembly import:

[reflection.assembly]::loadwithpartialname("System.Windows.Forms")  | Out-Null

enter image description here

x0n
  • 51,312
  • 7
  • 89
  • 111
markA
  • 1,609
  • 2
  • 17
  • 26

2 Answers2

3

PowerShell automatically loads the assembly from the same .Net version that PowerShell runs in. So pointing to a v2.0 assembly might still force it to load a v4.0 assembly.

Since your loading a v4.0 assembly, your probably not using PS1, but rather PS3 or 4, Then the easy solution would be to simply run PowerShell in 2.0 mode by using:

powershell.exe -version 2
Frode F.
  • 52,376
  • 9
  • 98
  • 114
  • Can you elaborate on this a bit? I'm not a powershell coder and all I know is I'm calling a .ps1 script file from an external tool in Visual Studio – markA Dec 03 '13 at 22:14
  • I'm not a programmer, so I don't know how you're using it in VS. If you run it with VS, why bother with a PS script with GUI when you could use c#? Run the command above in a cmd.exe window. The parameter `-version 2` tells the powershell.exe process to run with the v2.0 PS version (and CLR). If you only want powershell to run a script, try `powershell.exe -version 2 -file myscript.ps1` – Frode F. Dec 03 '13 at 22:40
  • I got it, just had to add the "-version 2" to the beginning of the arguments. To answer your question its not my script and I'm not a C# programmer either. It was a very convenient way to move lots of dtsx packages to many servers all at once. Still baffled as to why that event wouldn't work on .Net 4.0 but oh well thanks. – markA Dec 03 '13 at 22:45
2

Are you sure you are using PowerShell v1? That should be CLR v2 and not load .Net 4.0 assemblies. Your code should load the 2.0 assembly. If that is not the case, then your powershell v1 is configured to load CLR v4. That or you are using PowerShell v3 or higher.

Try $PSVersionTable and see what it reads.

If it reads PSVersion 1 or 2 then undo the change to PowerShell that made it use CLR v4. If it reads a higher version then there is no way. .Net framework assemblies will always load the 4.0 version.

In any case I don't expect it to explain your event not firing.

Update

As Graimer notes, you could however have newer PowerShell versions load as older versions, including older CLR versions.

Community
  • 1
  • 1
Lars Truijens
  • 42,837
  • 6
  • 126
  • 143
  • I have added a screen shot of my Version Table to my original post. I'm not sure what to do. You think I need to get a different powershell version? I checked it on the computers that are working and they have psVersion 2.0 and CLR 2.0 – markA Dec 03 '13 at 22:07
  • You have PowerShell v3. That explains what you are seeing. You could start powershell v3 as v2 if you want, as Graimer explains. I'll update my answer as well. – Lars Truijens Dec 03 '13 at 22:25