Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 向具有枚举的文件添加文本_C#_Visual Studio_Enums - Fatal编程技术网

C# 向具有枚举的文件添加文本

C# 向具有枚举的文件添加文本,c#,visual-studio,enums,C#,Visual Studio,Enums,我在尝试调用类对象时遇到问题,因此无法显示枚举和随后的消息。我已经完成了该类的所有代码(我想),但是在静态void main中遇到了问题。如何将枚举作为参数添加到tWrite方法中。我知道我的代码有问题,我只是不知道它在哪里。这是我目前的代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threadin

我在尝试调用类对象时遇到问题,因此无法显示枚举和随后的消息。我已经完成了该类的所有代码(我想),但是在静态void main中遇到了问题。如何将枚举作为参数添加到tWrite方法中。我知道我的代码有问题,我只是不知道它在哪里。这是我目前的代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;

namespace Logging
{
public class Log
{
    public enum Levels { Debug, Warn, Info, Error};

    private string path = @"C:\Users\eliotta1130\Desktop\CSharp\Labs\Logging.txt";

    //public string Path { get; set; }
    //public Levels Levels { get; set; }

    public Log(string path)
    {
        this.path = path;
        if(!File.Exists(path))
        {
            File.Create(path);
        }
        StreamWriter textOut = new StreamWriter( new FileStream(
                                                path, FileMode.Open, FileAccess.Write));
        textOut.WriteLine(DateTime.Now);

        textOut.Close();
    }
    public void tWrite(Levels levels, string message)
    {
        StreamWriter fs = new StreamWriter( new FileStream(path, FileMode.Append, FileAccess.Write));
        fs.WriteLine(string.Format("{0}, {1}", DateTime.Now.ToString("HH:mm:ss.ffff")), message);
        fs.Close();
    }

}
}
这里是我试图调用类对象的地方

namespace Logging
{
class Logging
{
    static void Main(string[] args)
    {
        Log files = new Log("Logging.txt");

        files.tWrite( , "Fix the problem");
    }
}

}

在另一个类中有枚举,因此可以这样调用它:

files.tWrite(Log.Levels.Warn, "Fix the problem");
files.tWrite(Levels.Warn, "Fix the problem");
或者将其移出类,但将其保留在同一命名空间中,并按如下方式调用它:

files.tWrite(Log.Levels.Warn, "Fix the problem");
files.tWrite(Levels.Warn, "Fix the problem");

Grant回答的另一种方法是在日志类中定义枚举实例:

 public Levels LogLevel {get;set;}

 static void Main(string[] args)
 {
    Log files = new Log("Logging.txt");
    files.LogLevel = Log.Levels.Warn;
    files.tWrite("Fix the problem");
 }

当然,您需要相应地修改tWrite()签名,但这不是重点。

您似乎没有在任何地方使用枚举级别。你为什么要费心把它放在tWrite的声明中呢?我没有因此而得到任何错误,但是出于某种原因,我在tWrite方法中得到了一个错误。它没有向文件写入任何内容。里面有什么不对劲吗?这就是它所说的“附加信息:索引(从零开始)必须大于或等于零,并且小于参数列表的大小”,它突出显示了tWrite方法中的fs.WriteLine。@GrantWinneyah,没有看到这一点。非常感谢。它现在没有创建文件吗?它成功运行,但未创建文件?我不确定我的代码在哪里抛出@这条路很好,我试过了,但仍然不起作用。隐马尔可夫模型。。。txt文件不存在,这就是为什么我有if语句来创建该文件,但我不确定它为什么不创建该文件@格兰特温尼