Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.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# 如何使用using指令包装streamreader?_C#_Asp.net_Streamreader_Using - Fatal编程技术网

C# 如何使用using指令包装streamreader?

C# 如何使用using指令包装streamreader?,c#,asp.net,streamreader,using,C#,Asp.net,Streamreader,Using,我有这段代码可以在一台WindowsServer2008R2上运行,但不能在新安装的Server2008R2上运行。下面的代码锁定了新服务器上的一些文本文件,但没有锁定旧服务器上的文本文件,这很奇怪。我非常确定这是一段代码,因为在执行之后,文件立即被w3wp进程锁定。我有一个closing()标记,但我想将代码转换为封装在 using(StreamReader objStreamReader = default(StreamReader)) { } 每次尝试封装时,我都会收到各种错误消息,

我有这段代码可以在一台WindowsServer2008R2上运行,但不能在新安装的Server2008R2上运行。下面的代码锁定了新服务器上的一些文本文件,但没有锁定旧服务器上的文本文件,这很奇怪。我非常确定这是一段代码,因为在执行之后,文件立即被w3wp进程锁定。我有一个closing()标记,但我想将代码转换为封装在

using(StreamReader objStreamReader = default(StreamReader))
{
}  
每次尝试封装时,我都会收到各种错误消息,因为有些东西只是变量,或者不能与using指令一起使用。如有任何帮助或建议,将不胜感激。此脚本的工作是解析文本文件,设置某些数据,例如下载到download.text引用,以便其他进程可以将解析后的数据输入到表中

public void read_file()
{
try
{   
//Open a file for reading
string FILENAME = Server.MapPath("\\gwi_client\\gwi_user_data\\" + ds_user_data.FieldValue("um_login", null) + ".txt" );

//Read the file, displaying its contents

//Get a StreamReader class that can be used to read the file
StreamReader objStreamReader = default(StreamReader);
objStreamReader = File.OpenText(FILENAME);

//Now, read the entire file into a string
string contents = objStreamReader.ReadToEnd();

//Below gets string for download used in bytes
int a = contents.IndexOf("download-used=") ;

int b = contents.IndexOf("upload-used=") ;

string contents_a = contents.Substring(a);

string used_values_a = contents_a.Replace(contents.Substring(b), " ") ;

string download_used_bytes = used_values_a.Replace("download-used="," ");

download_used_txt.Text = download_used_bytes ;  

//Below gets string for upload used in bytes
int c = contents.IndexOf("upload-used=") ;

int d = contents.IndexOf("last-seen=") ;

string contents_b = contents.Substring(c);

string used_values_b = contents_b.Replace(contents.Substring(d), " ") ;

string upload_used_bytes = used_values_b.Replace("upload-used="," ");

upload_used_txt.Text = upload_used_bytes ;

objStreamReader.Close();

}
catch (Exception ex)
{
Session["um_login"] = ds_user_data.FieldValue("um_login", null) ;
Session["sess_insert_um"] = "Y" ;

}
Session["um_login"] = ds_user_data.FieldValue("um_login", null) ;
Session["sess_insert_um"] = "Y" ;

}

你应该这样做:

using (StreamReader objStreamReader = File.OpenText(FILENAME))
{
   //code here
}

default(StreamReader)
将返回
null
,因为这是引用类型的默认值。谢谢!注释掉后//StreamReader objStreamReader=default(StreamReader)//objStreamReader=File.OpenText(文件名);然后利用你的建议,它似乎起了作用。