本人想在控制台程序里面使用webbrowser控件,实现两面载入,然后完成操作。测试代码如下:using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args) {
ArrayList listTemp = ReadFile("c:\\mytestdata\\inputtemp.txt"); DealAction(listTemp);
Console.ReadLine();
}
public static ArrayList ReadFile(String path)
{
try
{
ArrayList al = new ArrayList();
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(path))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
al.Add(line);
// Console.WriteLine(line);
}
}
return al;
// Console.ReadLine();
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
return null;
} } public static void DealAction(ArrayList listTemp)
{
if (listTemp.Count < 3)
{
Console.WriteLine(listTemp.Count);
return ;
}
WebBrowser browser = new WebBrowser();
}
}
}
但运行之后出现问题:未处理的“System.Threading.ThreadStateException”类型的异常出现在 System.Windows.Forms.dll 中。其他信息: 当前线程不在单线程单元中,因此无法实例化 ActiveX 控件“8856f961-340a-11d0-a96b-00c04fd705a2”。请问,我要如何实现控制台程序调用此控件?如果有源代码提供更好。
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args) {
ArrayList listTemp = ReadFile("c:\\mytestdata\\inputtemp.txt"); DealAction(listTemp);
Console.ReadLine();
}
public static ArrayList ReadFile(String path)
{
try
{
ArrayList al = new ArrayList();
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(path))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
al.Add(line);
// Console.WriteLine(line);
}
}
return al;
// Console.ReadLine();
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
return null;
} } public static void DealAction(ArrayList listTemp)
{
if (listTemp.Count < 3)
{
Console.WriteLine(listTemp.Count);
return ;
}
WebBrowser browser = new WebBrowser();
}
}
}
但运行之后出现问题:未处理的“System.Threading.ThreadStateException”类型的异常出现在 System.Windows.Forms.dll 中。其他信息: 当前线程不在单线程单元中,因此无法实例化 ActiveX 控件“8856f961-340a-11d0-a96b-00c04fd705a2”。请问,我要如何实现控制台程序调用此控件?如果有源代码提供更好。
WebBrowser browser = new WebBrowser();
这一行~
Create WebBrowser from console app
我还想问一个问题,此窗口可不可以隐藏?
代码如下,但没有效果,照样有窗口弹出来:
Form form = new Form();
form.Visible = false;
form.WindowState = FormWindowState.Maximized;
form.Controls.Add(browser);
form.Name = "Browser"; Application.Run(form);
form.Visible = false;
form.ShowInTaskbar = false;加上这三个就可以隐藏了