Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.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# 无法使用StreamReader方法_C#_String_Streamreader - Fatal编程技术网

C# 无法使用StreamReader方法

C# 无法使用StreamReader方法,c#,string,streamreader,C#,String,Streamreader,我正在尝试使用StreamReader方法读取文本文件,但它不起作用。我研究了所有的主题和论坛,但解决方案在我的项目中不起作用。我的文本文件位于c/users/user/documents/VS15/Projects/MyProject/here中 我得到这个错误: 字段初始值设定项不能引用非静态字段、方法或属性“MainPage.filename” 导致此错误的原因是什么?请尝试将其放入方法中,例如: static void Main(string[] args) { string f

我正在尝试使用StreamReader方法读取文本文件,但它不起作用。我研究了所有的主题和论坛,但解决方案在我的项目中不起作用。我的文本文件位于c/users/user/documents/VS15/Projects/MyProject/here中

我得到这个错误:

字段初始值设定项不能引用非静态字段、方法或属性“MainPage.filename”


导致此错误的原因是什么?

请尝试将其放入方法中,例如:

static void Main(string[] args)
{
    string filename = "text.txt";
    TextReader fi = new StreamReader(filename);
}
或者将变量设置为静态:

public static string filename = "text.txt";

尝试将其放入方法中,例如:

static void Main(string[] args)
{
    string filename = "text.txt";
    TextReader fi = new StreamReader(filename);
}
或者将变量设置为静态:

public static string filename = "text.txt";

这是一个编译时错误。将代码移动到方法体。更改为:

class C {

    string filename = "text.txt";
    TextReader fi = new StreamReader(filename);

    private void myMethod() {
        // ....
    }

}
致:

或将代码放入构造函数中:

class C {

    string filename = "text.txt";
    TextReader fi;

    public C() {
         fi = new StreamReader(filename);
    }

    private void myMethod() {
         // you can use the fi variable here
    }

}

这是一个编译时错误。将代码移动到方法体。更改为:

class C {

    string filename = "text.txt";
    TextReader fi = new StreamReader(filename);

    private void myMethod() {
        // ....
    }

}
致:

或将代码放入构造函数中:

class C {

    string filename = "text.txt";
    TextReader fi;

    public C() {
         fi = new StreamReader(filename);
    }

    private void myMethod() {
         // you can use the fi variable here
    }

}

但如果我不想使用方法呢?@Dimsn你说的“不想使用方法”是什么意思?那么你想把代码放在哪里?您能提供一些代码示例吗?publicstaticstringfilename=“razpisanie.txt”;TextReader fi=新的StreamReader(文件名);short n=fi.ReadLine();//然后我将使用这个文本文件中的那个变量和其他变量到我的程序中。我不必有这样的方法。它说“参数1:无法从'string'转换为'System.IO.Stream'”公共密封部分类MainPage:Page-这就是我的code@Dimsn不能在方法体之外使用fi.ReadLine()。你不能改变它。但如果我不想使用方法怎么办?@Dimsn你说的“不想使用方法”是什么意思?那么你想把代码放在哪里?您能提供一些代码示例吗?publicstaticstringfilename=“razpisanie.txt”;TextReader fi=新的StreamReader(文件名);short n=fi.ReadLine();//然后我将使用这个文本文件中的那个变量和其他变量到我的程序中。我不必有这样的方法。它说“参数1:无法从'string'转换为'System.IO.Stream'”公共密封部分类MainPage:Page-这就是我的code@Dimsn不能在方法体之外使用fi.ReadLine()。您不能更改它。现在它说“指针和固定大小的缓冲区只能在不安全的上下文中使用。”和“参数1:无法从'string'转换为'System.IO.Stream'”公共静态字符串filename=“razpisanie.txt”;TextReader fi=新的StreamReader(文件名);short n=fi.ReadLine();//然后我将使用这个文本文件中的那个变量和其他变量到我的程序中。我不必有这样的方法。它说“参数1:无法从'string'转换为'System.IO.Stream'”为True,因为fi.ReadLine()返回字符串,但如果您尝试将其转换为short,请尝试此“string n=fi.ReadLine()”我已经完成了。它说我不能再从string转换到System.IO.Stream。它说是在那个字符串上还是在别的字符串上?TextReader fi=新的StreamReader(文件名);现在它说“指针和固定大小的缓冲区只能在不安全的上下文中使用。”和“参数1:无法从'string'转换为'System.IO.Stream'”公共静态字符串filename=“razpisanie.txt”;TextReader fi=新的StreamReader(文件名);short n=fi.ReadLine();//然后我将使用这个文本文件中的那个变量和其他变量到我的程序中。我不必有这样的方法。它说“参数1:无法从'string'转换为'System.IO.Stream'”为True,因为fi.ReadLine()返回字符串,但如果您尝试将其转换为short,请尝试此“string n=fi.ReadLine()”我已经完成了。它说我不能再从string转换到System.IO.Stream。它说是在那个字符串上还是在别的字符串上?TextReader fi=新的StreamReader(文件名);