1

我是一名 C++ 开发人员,最近开始开发 C# WPF 应用程序。我正在关注 MVVM。好吧,我正在研究组合框和按钮。基本上我有一个浏览按钮来加载一个 binfile 并将其存储在组合框中,当我单击 TEST 按钮时,我应该读取文件中存在的数据,获取它的大小。

这是 XAML:

<ComboBox Name="ClockYHZBox" >              
          <ComboBoxItem Content="{Binding FirmwarePath}" />
</ComboBox>
<Button Content="Browse" Command="{Binding WriteFilePathCommand}" Name="RunPCMPDM0" />

<Button Content="Test" Command="{Binding WriteDataTestCommand}" />

视图模型类:

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          
    }

这就是我在 C++ 应用程序中所做的:

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);

正如您在上面看到的,它打开文件,将大小存储在 中fileSize,将内存块分配给缓冲区等等。我怎样才能实现它?我会很感激你的帮助:)

4

1 回答 1

2
byte[] b;
FileStream fileStream=new FileStream(FirmwarePath,FileMode.Open);
using (BinaryReader br = new BinaryReader(fileStream))
{
   b = br.ReadBytes(fileSize);
}
于 2012-11-01T11:54:54.000 回答