写了一个服务,目的就隔10秒调用一次一个网页,按f5没出什么问题;开始运行后说:windows服务必须要用installutil安装,但是我却不知道怎么安装;用installutils安装哪个exe文件呢?是按f5生成还是按shift+f6或者是发布生成得哪个exe呢?(以上都试过都说Exception occurred while initializing the installation:
System.BadImageFormatException: The format of the file 'Info_center_sender_trigg
er.vshost.exe' is invalid..
)。附主程序:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Threading;
namespace WindowsService1
{
public partial class Sendertrigger : ServiceBase
{
private Textoperator rcdsvctxt;
public string log_filename = "c:\\sender_trigger_log.txt";
public Sendertrigger()
{
InitializeComponent();
rcdsvctxt= new Textoperator();
}
public void process_main()
{
http_request httpins = new http_request("http://192.168.1.177/c.php");
String httpgontent = httpins.get_url();
if (httpgontent.Contains("fail")) {
this.rcdsvctxt.write_str(this.log_filename, "An error occured at " + DateTime.Now.ToLongTimeString() + " " + DateTime.Now.ToLongDateString());
}
Thread.Sleep(10);
this.process_main();
}
protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。
this.rcdsvctxt.write_str(this.log_filename, "Service start at " + DateTime.Now.ToLongTimeString() + " " + DateTime.Now.ToLongDateString());
} protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
DateTime now = System.DateTime.UtcNow;
this.rcdsvctxt.write_str(this.log_filename, "Service stoped at " + DateTime.Now.ToLongTimeString() + " " + DateTime.Now.ToLongDateString());
}
}
}————————————————————————————————————————
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Cache;
using System.Collections.Generic;namespace WindowsService1
{
class http_request
{
private String url;
public http_request(String url)
{
this.url = url;
}
public String get_url()
{
Uri myUri = new Uri(this.url);
// Create a new request to the above mentioned URL.
WebRequest myWebRequest = WebRequest.Create(myUri);
// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
RequestCachePolicy policy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
//从不缓存或者使用缓存的缓存策略:NoCacheNoStore
myWebRequest.CachePolicy = policy;
//给myWebRequest应用缓存策略
WebResponse myWebResponse = myWebRequest.GetResponse();
//取得响应
Stream rspstream = myWebResponse.GetResponseStream();
//取得响应流
StreamReader strreader = new StreamReader(rspstream, true);
//读取流
string tmp = "";
while (!strreader.EndOfStream)
{
tmp += strreader.ReadLine();
}
return tmp;
}
}
}
————————————————————————————————————————————
using System;
using System.IO;
namespace WindowsService1
{
class Textoperator
{
public void write_str(String filename, String str)
{
//filename是一个完整的路径
if (!File.Exists(filename))
{
File.Create(filename);
}
StreamWriter w = File.AppendText(filename);
w.WriteLine(str);
w.Flush();
w.Close();
}
}
}
————————————————————————————————————————————
原来是写php得,所以问题弱智,谢谢
System.BadImageFormatException: The format of the file 'Info_center_sender_trigg
er.vshost.exe' is invalid..
)。附主程序:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Threading;
namespace WindowsService1
{
public partial class Sendertrigger : ServiceBase
{
private Textoperator rcdsvctxt;
public string log_filename = "c:\\sender_trigger_log.txt";
public Sendertrigger()
{
InitializeComponent();
rcdsvctxt= new Textoperator();
}
public void process_main()
{
http_request httpins = new http_request("http://192.168.1.177/c.php");
String httpgontent = httpins.get_url();
if (httpgontent.Contains("fail")) {
this.rcdsvctxt.write_str(this.log_filename, "An error occured at " + DateTime.Now.ToLongTimeString() + " " + DateTime.Now.ToLongDateString());
}
Thread.Sleep(10);
this.process_main();
}
protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。
this.rcdsvctxt.write_str(this.log_filename, "Service start at " + DateTime.Now.ToLongTimeString() + " " + DateTime.Now.ToLongDateString());
} protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
DateTime now = System.DateTime.UtcNow;
this.rcdsvctxt.write_str(this.log_filename, "Service stoped at " + DateTime.Now.ToLongTimeString() + " " + DateTime.Now.ToLongDateString());
}
}
}————————————————————————————————————————
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Net.Cache;
using System.Collections.Generic;namespace WindowsService1
{
class http_request
{
private String url;
public http_request(String url)
{
this.url = url;
}
public String get_url()
{
Uri myUri = new Uri(this.url);
// Create a new request to the above mentioned URL.
WebRequest myWebRequest = WebRequest.Create(myUri);
// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
RequestCachePolicy policy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
//从不缓存或者使用缓存的缓存策略:NoCacheNoStore
myWebRequest.CachePolicy = policy;
//给myWebRequest应用缓存策略
WebResponse myWebResponse = myWebRequest.GetResponse();
//取得响应
Stream rspstream = myWebResponse.GetResponseStream();
//取得响应流
StreamReader strreader = new StreamReader(rspstream, true);
//读取流
string tmp = "";
while (!strreader.EndOfStream)
{
tmp += strreader.ReadLine();
}
return tmp;
}
}
}
————————————————————————————————————————————
using System;
using System.IO;
namespace WindowsService1
{
class Textoperator
{
public void write_str(String filename, String str)
{
//filename是一个完整的路径
if (!File.Exists(filename))
{
File.Create(filename);
}
StreamWriter w = File.AppendText(filename);
w.WriteLine(str);
w.Flush();
w.Close();
}
}
}
————————————————————————————————————————————
原来是写php得,所以问题弱智,谢谢
解决方案 »
- 【基础问题】 int v = 1;object b = v;b.GetType().IsValueType 为什么还是值类型
- 请问如何得到实例集合
- 有关弹出窗口问题
- 请教:sqlserver2005如何触发C#前台程序
- 如何检测 操作系统的 语言版本?比如英文,简体中文还是繁体中文?
- 这种是什么乱码 "学生"---"ѧÉú" "教师"---"½Ìʦ"
- c#调用com的问题?
- 如何把一个string类型的变量去除头和尾所有的空字符?
- 怪极了,其它文件运行正常,就运行这个文件报磁盘空间不够?
- GDI+编程:怎样将图像缩放
- 一段简单的代码,编译的时候出错,请专家出手相救
- 请问只申请了HTML空间,可不可以做静态的留言板,就象在静态网页的新闻中发表评论那样?
http://blog.csdn.net/knight94/archive/2006/03/17/627298.aspx
“点击项目-添加组件-程序类”吗?不对吧?程序能调试完成,运行时说是要用intallutil.exe安装。
http://community.csdn.net/Expert/TopicView3.asp?id=4732264
我用得是vs.net 2005
我先创建service工程,编好了代码。然后点击菜单栏-》项目-》添加新项,然后选择
"安装程序类"。
然后出来两个.cs,修改它们成下面得的代码:
intaller1.cs
——————————————————————————————————
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;namespace WindowsService1
{
[RunInstaller(true)]
public partial class Installer1 : Installer
{
private ServiceInstaller serviceInstaller;
private ServiceProcessInstaller processInstaller;
public Installer1()
{
InitializeComponent();
processInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller(); // Service will run under system account
processInstaller.Account = ServiceAccount.LocalSystem;
// Service will have Start Type of Manual serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.ServiceName = "Infomation center sender trigger"; Installers.Add(serviceInstaller);
Installers.Add(processInstaller);
}
}
}
——————————————————————————————————————————
installer1.designer1.cs
namespace WindowsService1
{
partial class Installer1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region 组件设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
} #endregion
}
}按f5,所有代码被锁定,然后弹了个对话窗口,大概说是要用intallutil安装完成;
我察看项目文件夹发现bin 目录和obj目录下都多了两个目录:debug和release,都只有release目录下有exe文件,我执行:
C:\Documents and Settings\Administrator>installutil "……\Projects\WindowsService1\WindowsService1\obj\Release\Info_center_sender_trigger.exe"Microsoft (R) .NET Framework Installation utility Version 1.1.4322.573
Copyright (C) Microsoft Corporation 1998-2002. All rights reserved.Exception occurred while initializing the installation:
System.BadImageFormatException: The format of the file 'Info_center_sender_trigger.exe' is invalid..点击_center_sender_trigger.exe执行,结果还是那个要求用installutil安装得信息。
怎么办呢?
我察看项目文件夹发现bin 目录和obj目录下都多了两个目录:debug和release,都只有release目录下有exe文件,我执行:service程序不能在vs环境直接进行调试