小编Bla*_*tor的帖子

本地计算机上的Windows服务已启动,然后停止错误

通常,我收到此错误:(本地计算机上的"服务名称"服务启动然后停止.如果我的代码出现问题,如某些服务未被其他服务或程序使用,则会自动停止驱动路径等.Windows服务无法启动.

如果达到大小限制,我有一个备份文件夹/文件的Windows服务到一个位置.详细信息都是由Windows服务在启动时读取的XML配置提供的.我有一个单独的窗体,有一个按钮,完全符合我的Windows服务的启动.在将其放入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)
                { …
Run Code Online (Sandbox Code Playgroud)

c# windows-services

96
推荐指数
3
解决办法
18万
查看次数

存储到数组时删除空格

测试代码:

string files = "C:\Hello; C:\Hi; D:\Goodmorning; D:\Goodafternoon; E:\Goodevening";
string[] paths = files.Split(';');

foreach (string s in paths)
{
    MessageBox.Show(s);
}
Run Code Online (Sandbox Code Playgroud)

如何在将空格存储到数组之前删除空格?

c# arrays trim

6
推荐指数
2
解决办法
1万
查看次数

在C#中将字符串转换为double或任何数值类型变量

我有一个读取xml文件的程序.我已经有一个代码读取特定元素并将其放在一个变量中.唯一缺少的是将元素(数字)与double变量进行比较,但元素被视为字符串.我在Google中看到的只是将double转换为string.你如何将字符串转换为双倍?

编辑:

我现在已经得到了,我无法回答我自己的问题,因为我仍然声名狼借.这就是我到目前为止所做的工作:

string stringNum = "2";
double value = double.Parse(stringNum);
Run Code Online (Sandbox Code Playgroud)

c# string double

2
推荐指数
1
解决办法
923
查看次数

检查XML中是否存在元素

XML:

<CONFIGURATION>
<Files>
    <File>D:\Test\TestFolder\TestFolder1\TestFile.txt</File>
    <File>D:\Test\TestFolder\TestFolder1\TestFile01.txt</File>
    <File>D:\Test\TestFolder\TestFolder1\TestFile02.txt</File>
    <File>D:\Test\TestFolder\TestFolder1\TestFile03.txt</File>
    <File>D:\Test\TestFolder\TestFolder1\TestFile04.txt</File>
</Files>
<SizeMB>3</SizeMB>
<BackupLocation>D:\Log backups\File backups</BackupLocation>
</CONFIGURATION>
Run Code Online (Sandbox Code Playgroud)

码:

private void btnLinq_Click(object sender, EventArgs e)
    {
        queryData(@"D:\WatchMe1\backupconfig1.xml");
    }

static void queryData(string xmlFile)
    {
        var xdoc = XDocument.Load(xmlFile);
        var configuration = xdoc.Element("CONFIGURATION");
        string sizeMB = configuration.Element("SizeMB").Value;
        string backupLocation = configuration.Element("BackupLocation").Value;
        //need a code here to check if element <File> exist before executing the file array
        string[] files = configuration.Element("Files").Elements("File").Select(c => c.Value).ToArray();

        foreach (string file in files)
        {
            Console.WriteLine(file);
        }
    }
Run Code Online (Sandbox Code Playgroud)

我有一个xml编写器程序,编辑上面的xml.Files元素可以更改为Folder元素.我有另一个程序读取值(文件位置)并用它做一些事情,我必须首先检查元素是文件或文件夹元素.

c# xml linq-to-xml

2
推荐指数
1
解决办法
1万
查看次数

在for循环中使用相同的对象 - C#

在此scnario中,程序将xmlfiles放在目录中.如果已经在listToWatch列表中添加了xmlfile,则会在第二种方法中对其进行评估.但是,firstMethod也会循环用于评估每个目录(下面没有写入).

该程序检测已添加的xml文件中的所有文件.但是如果程序进入另一个目录(因为firstMethod在另一个类中循环),则传递listToWatch = new List(),删除先前的listToWatch并创建一个新对象.

我希望使用相同的对象而不会被新列表覆盖.我不能把listToWatch =新名单secondMethod因为有一个for循环,系统将仅覆盖listToWatch一个新的对象.我不能把它放在firstMethod中,因为它需要在secondMethod中设置.我也不能把它放在类Sample中.我应该把listToWatch = new List()放在哪里?

class Sample
{
    public static List<string> listToWatch
    {
        get;
        set;
    }

    public static void firstMethod()
    {
        string getFiles = Directory.GetFiles(directory, "*.xml");
        foreach (string xmlFile in getFiles)
        {
            secondMethod(xmlFile);
        }
    }

    public static void secondMethod(xmlFile)
    {
        listToWatch = new List<string>();
        foreach (string file in xmlFile)
        {
            if (listToWatch.Contains(file))
            {
                sw.WriteLine(file + " is already added!");
            }
            else
            {
                listToWatch.add();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

c# oop object

2
推荐指数
1
解决办法
200
查看次数

将List传递给另一个类

我知道这里的一些代码不清楚.我还在尝试尝试一些事情.我有三个问题,为什么在我的列表中添加字符串时会出现错误?如何将Class1中的List传递给我的主类?我的语法在List passArr中是否正确?不确定我是否应该在passArr的末尾添加括号.

class Class1
{
    public static List<string> passArr
    {
        get;
        set;
    }

    public static void passIt()
    {
        passArr.Add("A"); //Error: Object reference not set to an instance of an object
    }
}
Run Code Online (Sandbox Code Playgroud)

主类

class Program
{
    static void Main(string[] args)
    {
        Class1.passIt();
        List<string> passArr1 = Class1.passArr;
        foreach (string s in passArr1)
        {
            Console.WriteLine(s);
        }
        Console.ReadLine();
    }
}
Run Code Online (Sandbox Code Playgroud)

c# list

0
推荐指数
1
解决办法
5908
查看次数

标签 统计

c# ×6

arrays ×1

double ×1

linq-to-xml ×1

list ×1

object ×1

oop ×1

string ×1

trim ×1

windows-services ×1

xml ×1