System.AppDomain objAPDom = System.AppDomain.CurrentDomain;
object obj = objAPDom.GetData("APP_LAUNCH_URL");
“name”的值 | 属性 |
---|---|
"APPBASE" | ApplicationBase |
"APP_CONFIG_FILE" | ConfigurationFile |
"DYNAMIC_BASE" | DynamicBase |
"DEV_PATH" | (无属性) |
"APP_NAME" | ApplicationName |
"CACHE_BASE" | PrivateBinPath |
"BINPATH_PROBE_ONLY" | PrivateBinPathProbe |
"SHADOW_COPY_DIRS" | ShadowCopyDirectories |
"FORCE_CACHE_INSTALL" | ShadowCopyFiles |
"CACHE_BASE" | CachePath |
(应用程序特定的) | LicenseFile |
错误代码:
//应用程序域
System.AppDomain objAPDom = System.AppDomain.CurrentDomain;
object obj = objAPDom.GetData("APP_LAUNCH_URL");
if (obj == null)
{
strURL = "";
}
else
{
strURL = obj.ToString();
}
首先需要导入 C:\WINDOWS\System32\mshtml.tlb、Interop.SHDocVw.dll两个动态库文件
-收缩
C#
代码/// <summary>
/// 返回指定Url的IE窗口下的 IHTMLDocument2 对象。
/// </summary>
/// <returns>IHTMLDocument2</returns>
public static IHTMLDocument2 GetIHTMLDocument2ByUrl(string url)
{
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();
foreach (SHDocVw.InternetExplorer ie in shellWindows)
{
string filename = System.IO.Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
if (filename.Equals("iexplore") && ie.LocationURL == url)
{
return ie.Document as IHTMLDocument2;
}
}
}
/// 返回指定Url的IE窗口下的 IHTMLDocument2 对象。
/// </summary>
/// <returns>IHTMLDocument2</returns>
public static IHTMLDocument2 GetIHTMLDocument2ByUrl(string url)
{
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();
foreach (SHDocVw.InternetExplorer ie in shellWindows)
{
string filename = System.IO.Path.GetFileNameWithoutExtension(ie.FullName).ToLower();
if (filename.Equals("iexplore") && ie.LocationURL == url)
{
return ie.Document as IHTMLDocument2;
}
}
}
通过 GetIHTMLDocument2ByUrl 方法可以获取已打开的IE窗口中指写地址的窗口中的 IHTMLDocument2 对象。
利用这个对象,就可以进行相关操作。
1.填写表单
-收缩
C#
代码IHTMLDocument2 iHTMLDocument2 = GetIHTMLDocument2ByUrl("http://www.163.com");
IHTMLInputElement input = (IHTMLInputElement)iHTMLDocument2.all.item("Username", 0); // 获取指定名称的对象
input.value = "Xiao"; // 赋值
IHTMLInputElement input = (IHTMLInputElement)iHTMLDocument2.all.item("Username", 0); // 获取指定名称的对象
input.value = "Xiao"; // 赋值
2.点击按钮
-收缩
C#
代码IHTMLDocument2 iHTMLDocument2 = GetIHTMLDocument2ByUrl("http://www.163.com");
HTMLDocumentClass obj = (HTMLDocumentClass)iHTMLDocument2;
IHTMLElement iHTMLElement = null;
IHTMLElementCollection c = obj.getElementsByTagName("input");
foreach (IHTMLElement e in c)
{
if (e.outerHTML.IndexOf("登录") != -1)
{
iHTMLElement = e;
break;
}
}
if (iHTMLElement != null)
{
iHTMLElement.click(); // 点击登录按钮
}
HTMLDocumentClass obj = (HTMLDocumentClass)iHTMLDocument2;
IHTMLElement iHTMLElement = null;
IHTMLElementCollection c = obj.getElementsByTagName("input");
foreach (IHTMLElement e in c)
{
if (e.outerHTML.IndexOf("登录") != -1)
{
iHTMLElement = e;
break;
}
}
if (iHTMLElement != null)
{
iHTMLElement.click(); // 点击登录按钮
}
Winform获取当前IEURL地址
- System.Diagnostics.Process[] _List = System.Diagnostics.Process.GetProcessesByName("iexplore");
- [DllImport("user32.dll")]
- public static extern int EnumChildWindows(IntPtr hWndParent, EnumWindowsProc ewp, int lParam);