如何使用 using 指令包装流阅读器?
How to wrap a streamreader with the using directive?
我的代码适用于一个 Windows Server 2008 R2,但不适用于全新的 Server 2008 R2 安装。下面的代码在新服务器上锁定了一些文本文件,但没有锁定旧服务器上的文本文件,这很奇怪。我非常确定这是这段代码,因为执行文件后立即被 w3wp 进程锁定。我有一个 closing() 标签,但我想将代码转换为封装在
中
using(StreamReader objStreamReader = default(StreamReader))
{
}
我每次尝试封装时都会收到各种错误消息,因为有些东西只是变量,或者不能与 using 指令一起使用。任何帮助或建议将不胜感激。该脚本的工作是解析设置某些数据的文本文件,例如下载到 download.text 引用,以便其他进程可以将解析的数据输入到 table.
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
}
我的代码适用于一个 Windows Server 2008 R2,但不适用于全新的 Server 2008 R2 安装。下面的代码在新服务器上锁定了一些文本文件,但没有锁定旧服务器上的文本文件,这很奇怪。我非常确定这是这段代码,因为执行文件后立即被 w3wp 进程锁定。我有一个 closing() 标签,但我想将代码转换为封装在
中using(StreamReader objStreamReader = default(StreamReader))
{
}
我每次尝试封装时都会收到各种错误消息,因为有些东西只是变量,或者不能与 using 指令一起使用。任何帮助或建议将不胜感激。该脚本的工作是解析设置某些数据的文本文件,例如下载到 download.text 引用,以便其他进程可以将解析的数据输入到 table.
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
}