- Back to Home »
- C#.NET »
- C# code to read content from notepads using System.IO.StreamReader
Posted by :
Sudhir Chekuri
Saturday, 5 October 2013
C# code to read content from notepads present in a directory(folder) and retreiving a part of content to save in database
string mystring = "";
DirectoryInfo dir = new DirectoryInfo(textBox1.Text);
var files = dir.GetFiles("Windows To Go Recovery Key*");
int i = files.Count();
if (i > 0)
{
foreach (var file in files)
{
string filename = file.ToString();
System.IO.StreamReader myfile = new System.IO.StreamReader(textBox1.Text+"/" + filename);
mystring = myfile.ReadToEnd();
int start = mystring.IndexOf("{");
int end = mystring.IndexOf("}");
string rid = mystring.Substring(start + 1, end - start - 1);
int kstart = mystring.IndexOf("y: ");
string key = mystring.Substring(kstart + 3, 55);
SqlConnection con = new SqlConnection("Data Source=" + TxtServername.Text + ";Initial Catalog=WindowsToGO;Integrated Security=True");
SqlCommand cmd = new SqlCommand("use WindowsToGO insert into [dbo].[RecoverAndHardwareCore-Keys] values('"+rid.ToString()+"','"+key.ToString()+"')", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
}
MessageBox.Show("IDs and Keys successfully created");
}
DirectoryInfo dir = new DirectoryInfo(textBox1.Text);
var files = dir.GetFiles("Windows To Go Recovery Key*");
int i = files.Count();
if (i > 0)
{
foreach (var file in files)
{
string filename = file.ToString();
System.IO.StreamReader myfile = new System.IO.StreamReader(textBox1.Text+"/" + filename);
mystring = myfile.ReadToEnd();
int start = mystring.IndexOf("{");
int end = mystring.IndexOf("}");
string rid = mystring.Substring(start + 1, end - start - 1);
int kstart = mystring.IndexOf("y: ");
string key = mystring.Substring(kstart + 3, 55);
SqlConnection con = new SqlConnection("Data Source=" + TxtServername.Text + ";Initial Catalog=WindowsToGO;Integrated Security=True");
SqlCommand cmd = new SqlCommand("use WindowsToGO insert into [dbo].[RecoverAndHardwareCore-Keys] values('"+rid.ToString()+"','"+key.ToString()+"')", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
}
MessageBox.Show("IDs and Keys successfully created");
}