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

C# 无法在Combobox中存储文件并读取数据,大小以WPF表示 我是一个C++开发人员,最近开始研究C语言的WPF应用程序。我正在关注MVVM。我正在做组合框和按钮。基本上,我有一个浏览按钮来加载binfile并将其存储在combobox中,当我单击TEST按钮时,我应该读取文件中的数据,获得它的大小,c#,.net,wpf,mvvm,combobox,C#,.net,Wpf,Mvvm,Combobox,这是XAML: <ComboBox Name="ClockYHZBox" > <ComboBoxItem Content="{Binding FirmwarePath}" /> </ComboBox> <Button Content="Browse" Command="{Binding WriteFilePathCommand}" Name="RunPCMPDM0" /> <Button Co

这是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          
    }

如上所示,它打开文件,将大小存储在
fileSize
中,将内存块分配给缓冲区,等等。我怎样才能做到呢?非常感谢您的帮助:)

我认为
Open
不是第二个参数。您需要的是一个字节[],而不是单个字节。字节[]可以逐字节迭代和操作。您发布的代码只是将其复制到上面的缓冲区中。您将不得不发布更多:)让我们
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);
byte[] b;
FileStream fileStream=new FileStream(FirmwarePath,FileMode.Open);
using (BinaryReader br = new BinaryReader(fileStream))
{
   b = br.ReadBytes(fileSize);
}