I am a C++ developer and recently started working on C# WPF app. I am following MVVM. Well I am working on Combobox and buttons. Basically I have a browse button to load a binfile and store it in combobox and when I click TEST button, i should read the data present in file, get the size of it.
Here is the XAML:
<ComboBox Name="ClockYHZBox" >
<ComboBoxItem Content="{Binding FirmwarePath}" />
</ComboBox>
<Button Content="Browse" Command="{Binding WriteFilePathCommand}" Name="RunPCMPDM0" />
<Button Content="Test" Command="{Binding WriteDataTestCommand}" />
ViewModel Class:
private string _selectedFirmware;
public string FirmwarePath
{
get; set;
}
// This method gets called when BROWSE Button is pressed
private void ExecuteWriteFileDialog()
{
var dialog = new OpenFileDialog { InitialDirectory = _defaultPath };
dialog.DefaultExt = ".bin";
dialog.Filter = "BIN Files (*.bin)|*.bin";
dialog.ShowDialog();
FirmwarePath = dialog.FileName; // Firmware path has the path
}
// Method gets called when TEST Button is Pressed
public void mSleepTestCommandExecuted()
{
int cmd = (22 << 8) | 0x06;
System.IO.StreamReader sr = new System.IO.StreamReader(FirmwarePath);
string textdata = sr.ReadToEnd();
int fileSize = (int)new System.IO.FileInfo(FirmwarePath).Length;
Byte[] buffer = new Byte[256];
// This gives me the size and data, But i am failing to do
// further operation of storing value in BUFFER
// and MEMCPY which is shown below in C++ Code
}
This is how I had done in my C++ application:
MemoryBlock binFile;
m_wdbFile->getCurrentFile().loadFileAsData(binFile); //m_wbdFile is a filedialog object
BYTE *buffer = NULL;
int fileSize = binFile.getSize();
buffer = (BYTE *)calloc(sizeof(BYTE), fileSize + 2);
memcpy(buffer+2, binFile.getData(), fileSize);
As you can see above, it opens the file, stores the size in fileSize, allocates block of memory to buffer and so on. How can I achieve it? I would appreciate your help 🙂
1 Answer