Showing results for 
Search instead for 
Do you mean 

Why doesn't this work?

Here is my code


using System;
using System.Collections;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Test
public class UnitTest1

public void TestMethod1()
System.Environment.SetEnvironmentVariable("SESSIONNAME", "Console");
System.Environment.SetEnvironmentVariable("UGII_UGOPENPP", @"C:\Program Files\Siemens\NX 9.0\ugopenpp");
System.Environment.SetEnvironmentVariable("UGII_USERFCN", @"C:\Program Files\Siemens\NX 9.0\ugopen");
System.Environment.SetEnvironmentVariable("UGII_LANG", "english");
System.Environment.SetEnvironmentVariable("UGII_JAVA_HOME", @"C:\Program Files\Java\jre1.8.0_25");
System.Environment.SetEnvironmentVariable("SPLM_LICENSE_SERVER", "28000@CTS-SERVER");
System.Environment.SetEnvironmentVariable("UGII_LICENSE_FILE", "27000@CTS-SERVER");
System.Environment.SetEnvironmentVariable("UGII_ENV_FILE", @"U:\NX90\Concept\NX90custom\NX90library\NX90env.dat");
System.Environment.SetEnvironmentVariable("UGII_BASE_DIR", @"C:\Program Files\Siemens\NX 9.0");
System.Environment.SetEnvironmentVariable("NX_NUMBER", "NX90");
System.Environment.SetEnvironmentVariable("UGII_ROUTING_KIT_UNITS", "metric");
System.Environment.SetEnvironmentVariable("UGII_ROUTE_UGMANAGER_MODE", "FALSE");
System.Environment.SetEnvironmentVariable("NX_CUSTOM_DIR", @"U:\NX90\Concept\NX90custom");
System.Environment.SetEnvironmentVariable("NX_CUSTOM_LIB", @"U:\NX90\Concept\NX90custom\NX90library");
System.Environment.SetEnvironmentVariable("NX_PACKAGE_NAME", "NX");
System.Environment.SetEnvironmentVariable("UGII_ROOT_DIR", @"C:\Program Files\Siemens\NX 9.0\ugii\");

NXOpen.Session ses = NXOpen.Session.GetSession();
NXOpen.PartLoadStatus status;
ses.Parts.OpenDisplay(@"G:\0Components\fasteners\metric\shcs\020\20mm-shcs-065.prt", out status);




My question is why does this raise a System.BadImageFormatException when I runt the test with the actual Microsoft.VisualStudio.TestTools.UnitTesting framework?

Are all my enviornment variables being set correctly? Am I missing one?

Is there something else I need to do?




Re: Why doesn't this work?

BadImageFormatException can mean that you built an image for Win32 or AnyCPU, instead of x64, or it might mean that you are trying to use the wrong version of the .Net Framework.


And a side note - we do not recommend setting environment variables within an NXOpen program.  We always suggest that you set them outside of the program first.  If you run your program from an NX Command Prompt, then they should all be set correctly for you.  If you are running inside the Visual Studio, then you should start the studio session from an NX Command Prompt, to inherit all the right settings.


Re: Why doesn't this work?


Follow the following process to resolve the issue.

1) Open the Test project from NX command shell.

2) Select Test->Test Settings->Default Processor architecture->x64


Now run the test. It will be success.