WEB开发网
开发学院软件开发Java java获取.net dll/exe的版本号 阅读

java获取.net dll/exe的版本号

 2012-05-15 09:50:08 来源:WEB开发网   
核心提示:package cn.cfets.utils;import static org.junit.Assert.assertEquals;import java.io.File;import org.junit.Test;public class AtfVersionTest {@Testpublic void testG
package cn.cfets.utils;

import static org.junit.Assert.assertEquals;

import java.io.File;

import org.junit.Test;

public class AtfVersionTest {

	@Test
	public void testGetDllVersion() {
		assertEquals("8.1.1.0", AtfVersion.getDllVersion(new File(
				"./data/DevExpress.Data.v8.1.dll")));
	}

	@Test
	public void testGetExeVersion() {
		assertEquals("1.0.0.0", AtfVersion.getExeVersion(new File(
				"./data/WindowsFormsApplication1.exe")));
	}

}

===========================================================

package cn.cfets.utils;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class AtfVersion {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	public static String getDllVersion(File item) {
		RandomAccessFile input = null;
		try {
			input = new RandomAccessFile(item, "r");
			input.seek(item.length() - 2);
			byte b1 = input.readByte();
			byte b2 = input.readByte();
			// find data: 0x00 0x41 (16 byte char 'A')
			while (!(b1 == (byte) 0 && b2 == (byte) 0x41)) {
				input.seek(input.getFilePointer() - 3);
				b1 = input.readByte();
				b2 = input.readByte();
			}
			input.seek(input.getFilePointer() - 2);

			// get "Assembly Version" and the version string
			StringBuilder[] sbs = new StringBuilder[2];
			for (int i = 0; i < sbs.length; i++) {
				sbs[i] = new StringBuilder();
				b1 = input.readByte();
				b2 = input.readByte();
				while (b2 != (byte) 0) {
					sbs[i].append((char) b2);
					b1 = input.readByte();
					b2 = input.readByte();
				}
				System.out.println(sbs[i]);
			}

			// return the version string
			if ("Assembly Version".equalsIgnoreCase(sbs[0].toString())) {
				return sbs[1].toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (input != null) {
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		// don't find version string, return a space
		return " ";
	}

	public static String getExeVersion(File item) {
		return getDllVersion(item);
	}
}

Tags:java 获取 net

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接