GetPrivateProfileString Windows API working in migrated code
Make sure INI files and any configuration file used in a migrated application is located in the right location.
Last updated
Make sure INI files and any configuration file used in a migrated application is located in the right location.
Last updated
When working with migrated applications from VB6 to .NET (C#/VB.NET) it's very important to pay attention to the usage of INI files or any configuration file consummed by the application at runtime.
The reason is very simple, when you're developing a VB6 aplication, it's possible those INI file are located at the same folder where the VB6 source code file is, but, when this source code is migrated using the VBUC, INI files or any other configuration files aren't moved to the target folder (the folder where migrated code is located after the initial migration).
It means, all those files needed by the migrated application, has to be manually moved to the right destination. And by "right destination" it should be understood as the place where .NET puts the new binary files of the migrated code.
The following example using GetPrivateProfileString windows API method in a VB6 application will illustrate this.
This VB6 app, reads a INI file located at the same path of the VB6 source code files:
The following code shows how this small application works: It opens sample.ini file and get the value "ExeName" located under "SME100" section.
When the VB6 source code is migrated, the target folder contains the new C#/VB.NET code (for this example, the code was converted to C#). No other additional files have being added (for instance, sample.ini).
The migrated code will look like:
As shown above, the GetPrivateProfileString Windows API definition has being moved to a new project where all API methods will be centralized.
If migrated code is executed, a runtime exception will be thrown by GetPrivateProfileString(). Reason: sample.ini file is not located (yet) at this path Path.GetDirectoryName(Application.ExecutablePath) + "\\sample.ini";
That line of code returns the location for the EXE file of the migrated application.
Solution is very simple, go to the folder where sample.ini file is located, copy the file and then put it into the folder where the EXE file of the migrated application is generated. For this example, sample.ini file has to be copied into the bin sub-folder of the target folder for the miration.
If you are deploying the migrated application in a different location do not forget moving INI files and any configuration resource file needed by your application.