通常情况下,我得到这个错误: (本地计算机上的“服务名称”服务启动,然后停止一些服务自动停止,如果他们不被其他服务或应用。程序),当我的代码出现问题时(例如不存在的驱动器路径等),Windows服务将无法启动。Windows服务启动,然后停止错误
我有备份的文件夹/文件,一个位置,如果它达到大小限制Windows服务。详细信息全部由Windows服务读取的XML配置提供。我有一个单独的窗体,它有一个按钮,它完全符合我的Windows服务的onstart所做的。我使用我的Windows窗体来调试代码,然后将其放入我的Windows服务中。
当我启动我的Windows窗体。它做它想做的事情。当我将我的代码放入Windows服务OnStart()方法时,出现错误。
这里是我的代码:
protected override void OnStart(string[] args)
{
private static string backupConfig = @"D:\LogBackupConfig\backupconfig.xml";
private static string serviceStat = @"D:\LogBackupConfig\Status.txt";
private static string fileFolderStat = @"D:\LogBackupConfig\FileFolderStat.txt";
protected override void OnStart(string[] args)
{
if (File.Exists(backupConfig))
{
FileSystemWatcher watcher = new FileSystemWatcher();
XmlTextReader reader = new XmlTextReader(backupConfig);
XmlNodeType type;
List<string> listFile = new List<string>();
string fileWatch = "";
//this loop is for reading XML elements and assigning to variables
while (reader.Read())
{
type = reader.NodeType;
if (type == XmlNodeType.Element)
{
if (reader.Name == "File")
{
reader.Read();
fileWatch = reader.Value;
}
else if (reader.Name == "Folder")
{
reader.Read();
fileWatch = reader.Value;
}
}
}
reader.Close();
watcher.Path = fileWatch;
watcher.Filter = "*.*";
//this loop reads whether the service will watch a file/folder
XmlTextReader reader1 = new XmlTextReader(backupConfig);
while (reader1.Read())
{
type = reader1.NodeType;
if (type == XmlNodeType.Element)
{
if (reader1.Name == "File")
{
watcher.IncludeSubdirectories = false;
watcher.Changed += new FileSystemEventHandler(OnChangedFile);
}
else if (reader1.Name == "Folder")
{
watcher.IncludeSubdirectories = true;
watcher.Changed += new FileSystemEventHandler(OnChangedFolder);
}
}
}
reader1.Close();
watcher.EnableRaisingEvents = true;
}
else
{
StreamWriter sw = new StreamWriter(serviceStat, true);
sw.WriteLine("File not found. Please start the Log Backup UI first.");
sw.Close();
}
}
我不知道是什么让Windows服务没有启动,Windows窗体模拟器的罚款。什么似乎是问题?
更新: 经过多次试验后,我发现只使用文件夹目录(w/out文件),windows服务不起作用。当我用特定文件(包括其目录)替换fileWatch变量时,Windows服务启动。当我将其更改回文件夹位置时,它不起作用。我认为文件夹位置在filewatcher中不起作用。
当我尝试创建一个新的Windows服务,观看文件夹的位置,它的工作..但是,当我在我原来的Windows服务中尝试相同的位置,它不工作!我是mindf $#ed!看来我必须创建一个新的Windows服务,并且每次我放置一个新的代码/函数时都要构建安装程序。这样我就可以跟踪出现错误的位置。
非常感谢!我试过了,这是错误:“服务无法启动System.ArgumentException:路径不是合法的形式 at System.IO.Path.NormalizePath(String path,Boolean fullCheck,Int32 maxPathLength) at System .IO.Path.GetFullPathInternal(String path) at System.IO.Path。GetFullPath(字符串路径) 在System.IO.FileSystemWatcher.StartRaisingEvents() 在System.IO.FileSystemWatcher.set_EnableRaisingEvents(布尔值) 在LogBackupWinSvc.Service1.OnStart(字串[] args) 在了System.ServiceProcess.ServiceBase。 ServiceQueuedMainCallback(Object state)“ – Blackator
Windows事件查看器显示了完整的堆栈跟踪,非常有帮助的工具 –