Reputation: 23
I have this code that works on one Windows Server 2008 R2 but not a fresh new server 2008 R2 install. The code below is locking some text files on the new server but not the old one which is very strange. I'm very sure it is this code because immediately after executing the files are locked by the w3wp process. I have a closing() tag but I would like to convert the code to be encapsulated in
using(StreamReader objStreamReader = default(StreamReader))
{
}
Every try I make at encapsulating I get various error messages as somethings are variables only or can not be used with the using directive. Any help or suggestions would be appreciated. The job of this script is to parse a text file setting certain data such as download to a download.text reference so other processes can input the parsed data into a 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" ;
}
Upvotes: 2
Views: 595
Reputation: 45500
You should do it like this:
using (StreamReader objStreamReader = File.OpenText(FILENAME))
{
//code here
}
Upvotes: 2