/*
Obtain the Oracle service name
1. Query the registry and obtain the Oracle Installation root directory
For example, HKEY_LOCAL_MACHINE \ SOFTWARE \ oracle \ ORACLE_HOME REG_SZ E: \ oracle \ ora92
Find the location of the Oracle service name file: root directory \ Network \ admin \ tnsnames. ora
2. Parse the file. The file structure is shown in figure
#------------------------------------------------
Portal =
(Description =
(Address_list =
(Address = (Protocol = TCP) (host = 134.104.52.6) (Port = 1521 ))
)
(CONNECT_DATA =
(SERVICE_NAME = portal)
)
)
3. Key points:
Retrieve and parse a row
Skip the comment line with the # hitting Header
After spaces are filtered out, the first character is ~ The line between Z contains the Oracle service name
Truncates the string on the left of the first "=" Number of the row. After trim is processed, it is the Oracle service name.
*/
Public static string [] getoracletnsnames ()
{
Try
{
// Query the registry and obtain the path of the Oracle service file
Registrykey key = registry. localmachine. opensubkey ("software"). opensubkey ("oracle ");
String home = (string) Key. getvalue ("ORACLE_HOME ");
String file = home + @ "\ Network \ admin \ tnsnames. ora ";
// Parse the file
String line;
Arraylist arr = new arraylist ();
Streamreader sr = new streamreader (File );
While (line = Sr. Readline ())! = NULL)
{
Line = line. Trim ();
If (line! = "")
{
Char c = line [0];
If (C> = 'A' & C <= 'Z ')
Arr. Add (line. substring (0, line. indexof ('')));
}
}
Sr. Close ();
// returns a string array
return (string []) Arr. toarray (typeof (string);
}< br> catch (exception ex)
{< br> return NULL;
}< BR >}