hi
i was wondering how can i import and export rdb files using c#
tried StreamReader doesn't work
thnx in advance
i was wondering how can i import and export rdb files using c#
tried StreamReader doesn't work
thnx in advance
public class Strings
{
static MainWindow MainWindow_Instance = MainWindow.Instance;
static Stopwatch FileLoadTimer = new Stopwatch();
static Header FileHeader = null;
public static byte[] RawBytes = null;
public static StringResource[] Data = null;
static int DataType = 0;
static int lastProgressValue = 0;
public static void ReadFile(string filePath)
{
string fileName = Path.GetFileNameWithoutExtension(filePath).ToLower();
using (BackgroundWorker loadWorker = new BackgroundWorker())
{
loadWorker.DoWork += (o, x) =>
{
FileLoadTimer.Start();
byte[] fieldBuffer = new byte[0];
int curRow = 0;
RawBytes = File.ReadAllBytes(filePath);
using (var ms = new MemoryStream(RawBytes))
{
FileHeader = new Header();
fieldBuffer = new byte[27];
ms.Read(fieldBuffer, 0, fieldBuffer.Length);
FileHeader.HeaderBytes = fieldBuffer;
ms.Position += 101;
fieldBuffer = new byte[4];
ms.Read(fieldBuffer, 0, 4);
FileHeader.FileRows = BitConverter.ToInt32(fieldBuffer, 0);
Data = new StringResource[FileHeader.FileRows];
loadWorker.ReportProgress(0);
int nameLen, valueLen;
while (true)
{
if (curRow == FileHeader.FileRows) { break; }
fieldBuffer = new byte[4];
ms.Read(fieldBuffer, 0, 4);
nameLen = BitConverter.ToInt32(fieldBuffer, 0);
fieldBuffer = new byte[4];
ms.Read(fieldBuffer, 0, 4);
valueLen = BitConverter.ToInt32(fieldBuffer, 0);
fieldBuffer = new byte[nameLen];
ms.Read(fieldBuffer, 0, fieldBuffer.Length);
Data[curRow].name = Encoding.Default.GetString(fieldBuffer);
fieldBuffer = new byte[valueLen];
ms.Read(fieldBuffer, 0, fieldBuffer.Length);
Data[curRow].value = Encoding.Default.GetString(fieldBuffer);
fieldBuffer = new byte[4];
ms.Read(fieldBuffer, 0, 4);
Data[curRow].code = BitConverter.ToInt32(fieldBuffer, 0);
fieldBuffer = new byte[4];
ms.Read(fieldBuffer, 0, 4);
Data[curRow].group_id = BitConverter.ToInt32(fieldBuffer, 0);
ms.Position += 16;
if (Math.Abs(curRow - lastProgressValue) > 2500) { loadWorker.ReportProgress(curRow); lastProgressValue = curRow; }
else if ((curRow + 1) == FileHeader.FileRows) { loadWorker.ReportProgress(curRow); lastProgressValue = curRow; }
curRow++;
}
}
};
loadWorker.WorkerReportsProgress = true;
loadWorker.ProgressChanged += (o, x) =>
{
if (x.ProgressPercentage == 0) { MainWindow_Instance.Progress.Maximum = FileHeader.FileRows; }
else { MainWindow_Instance.Progress.Value = x.ProgressPercentage; }
};
loadWorker.RunWorkerCompleted += (o, x) =>
{
FileLoadTimer.Stop();
MainWindow_Instance.Status.Text = string.Format("{0} Rows Loaded from {1} in: {2} ms", FileHeader.FileRows, fileName, FileLoadTimer.ElapsedMilliseconds);
GenerateTab(fileName);
};
MainWindow_Instance.Status.Text = string.Format("Loading: {0}", fileName);
loadWorker.RunWorkerAsync();
}
}
public static void ReadDB()
{
string connectionString = Settings.ConnectionString;
string selectStatement = "SELECT ";
int selectStatement_idx = 0;
PropertyInfo[] properties = typeof(StringResource).GetProperties();
foreach (PropertyInfo property in properties)
{
if (selectStatement_idx == properties.Length - 1) { selectStatement += string.Format("[{0}] ", property.Name); }
else { selectStatement += string.Format("[{0}], ", property.Name); }
selectStatement_idx++;
}
selectStatement += "FROM dbo.StringResource";
int rowCount = 0;
int curRow = 0;
using (BackgroundWorker loadWorker = new BackgroundWorker())
{
loadWorker.DoWork += (o, x) =>
{
FileLoadTimer = new Stopwatch();
FileLoadTimer.Start();
SqlConnection sqlCon = new SqlConnection(Settings.ConnectionString);
SqlCommand sqlCmd = new SqlCommand("SELECT COUNT(code) FROM dbo.StringResource", sqlCon);
SqlDataReader sqlRdr = null;
try
{
sqlCon.Open();
rowCount = (int)sqlCmd.ExecuteScalar();
Data = new StringResource[rowCount];
DataType = 1;
MainWindow_Instance.InitProgress(0, rowCount);
if (rowCount > 0)
{
sqlCmd = new SqlCommand(selectStatement, sqlCon);
sqlRdr = sqlCmd.ExecuteReader();
while (sqlRdr.Read())
{
Data[curRow].name = Convert.ToString(sqlRdr[0]);
Data[curRow].group_id = Convert.ToInt32(sqlRdr[1]);
Data[curRow].code = Convert.ToInt32(sqlRdr[2]);
Data[curRow].value = Convert.ToString(sqlRdr[3]);
curRow++;
if (Math.Abs(curRow - lastProgressValue) > 100) { loadWorker.ReportProgress(curRow); lastProgressValue = curRow; }
else if ((curRow + 1) == rowCount) { loadWorker.ReportProgress(curRow); lastProgressValue = curRow; }
}
}
}
catch (SqlException sqlEx)
{
MessageBox.Show(sqlEx.Message, "SQL Exception Occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exception Occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
sqlCon.Close();
}
};
loadWorker.WorkerReportsProgress = true;
loadWorker.ProgressChanged += (o, x) =>
{
MainWindow_Instance.Progress.Value = x.ProgressPercentage;
};
loadWorker.RunWorkerCompleted += (o, x) =>
{
FileLoadTimer.Stop();
MainWindow_Instance.Status.Text = string.Format("{0} Rows loaded from {1} in: {2} ms", Data.Length, "db_string", FileLoadTimer.ElapsedMilliseconds);
GenerateTab("db_string");
};
loadWorker.RunWorkerAsync();
}
}
public static void SaveData()
{
string rdbName = "db_string";
string saveDir = Properties.Settings.Default.outputDirectory;
string saveName = rdbName;
if (Properties.Settings.Default.appendAscii) { saveName += "(ascii).rdb"; }
else { saveName += ".rdb"; }
if (Properties.Settings.Default.saveHashed) { saveName = FileNameCipher.Encode(saveName); }
string savePath = Path.Combine(saveDir, saveName);
if (!Directory.Exists(saveDir))
{
Directory.CreateDirectory(saveDir);
}
try
{
using (BackgroundWorker saveWorker = new BackgroundWorker())
{
saveWorker.DoWork += (o, x) =>
{
FileLoadTimer = new Stopwatch();
FileLoadTimer.Start();
using (BinaryWriter bw = new BinaryWriter(File.Create(savePath)))
{
if (DataType != 0)
{
FileHeader = new Header();
FileHeader.HeaderBytes = new byte[128];
FileHeader.FileRows = Data.Length;
}
byte[] HeaderBytes = Encoding.Default.GetBytes("Teal.Sky.Project.2015.rEDIT");
bw.Write(HeaderBytes);
bw.Write(new byte[128 - HeaderBytes.Length]);
bw.Write(FileHeader.FileRows);
for (int curRow = 0; curRow < FileHeader.FileRows; curRow++)
{
bw.Write(Convert.ToInt32(Data[curRow].name.Length));
bw.Write(Convert.ToInt32(Data[curRow].value.Length));
bw.Write(Encoding.Default.GetBytes(Data[curRow].name));
bw.Write(Encoding.Default.GetBytes(Data[curRow].value));
bw.Write(Data[curRow].code);
bw.Write(Data[curRow].group_id);
bw.Write(new byte[16]);
if (Math.Abs(curRow - lastProgressValue) > 2500) { saveWorker.ReportProgress(curRow); lastProgressValue = curRow; }
else if ((curRow + 1) == FileHeader.FileRows) { saveWorker.ReportProgress(curRow); lastProgressValue = curRow; }
}
}
};
saveWorker.WorkerReportsProgress = true;
saveWorker.ProgressChanged += (o, x) =>
{
if (x.ProgressPercentage == 0) { MainWindow_Instance.Progress.Maximum = FileHeader.FileRows; }
else { MainWindow_Instance.Progress.Value = x.ProgressPercentage; }
};
saveWorker.RunWorkerCompleted += (o, x) =>
{
FileLoadTimer.Stop();
MainWindow_Instance.Status.Text = string.Format("{0} Rows saved to {1} in: {2} ms", Data.Length, saveName, FileLoadTimer.ElapsedMilliseconds);
};
saveWorker.RunWorkerAsync();
}
}
catch (Exception ex)
{
}
}
public static void GenerateTab(string title)
{
if (MainWindow_Instance.TabControl.TabPages[title] == null)
{
TabPage newPage = new TabPage
{
Name = title,
Text = title
};
MainWindow_Instance.TabControl.TabPages.Add(newPage);
MainWindow_Instance.TabControl.TabPages[title].Controls.Add(new DataGridView { Name = string.Format("{0}_dgv", title), Dock = DockStyle.Fill });
DataGridView tmpDGV = MainWindow_Instance.TabControl.TabPages[title].Controls[string.Format("{0}_dgv", title)] as DataGridView;
tmpDGV.DataSource = Data;
}
}
public static void ClearAssets()
{
FileHeader = null;
Data = null;
DataType = 0;
lastProgressValue = 0;
}
}
public struct StringResource
{
public String name { get; set; }
public Int32 group_id { get; set; }
public Int32 code { get; set; }
public String value { get; set; }
}