c# - 在 WPF 中单击按钮执行文件操作

标签 c# c++ .net wpf button

我是一名 C++ 开发人员,最近转向 WPF C# 领域。我正在开发一个应用程序,我必须在其中使用 fileopendialog 加载文本文件,选择文件并将其保存在组合框中,然后单击 writebutton 我应该使用文本文件中存在的数据执行一些操作。

我用 C++ 做了如下:

if(button == m_writeButton)
{
    // get the data from the file
    File m_binFile = m_fileChoice->getCurrentFile();
    MemoryBlock m_data;

    m_binFile.loadFileAsData(m_data);
    size_t blockSize = m_data.getSize();

    unsigned char *f_buffer;
    f_buffer = (unsigned char *)m_data.getData();
    unsigned cnt = 0;

    // Some code
}

我用 C# 做了如下:

<ComboBox Name="WriteFileCombo" >
                        <ComboBoxItem Content="Firmware To Download" />
                        <ComboBoxItem Content="{Binding FirmwarePath}" />
</ComboBox>
<Button Content="..." Command="{Binding Path=WriteFilePathCommand}" Name="FileDialogBtn"/>                       

<Button Content="Write File" Command="{Binding Path=WriteFileCommand}" Name="WriteFileBtn" />

查看模型类:

private string _selectedFirmware;
    public string FirmwarePath
    {
        get { return _selectedFirmware; }
        set
        {
            _selectedFirmware = value;
            OnPropertyChanged("FirmwarePath");
        }
    }

// Gets called when Browse Button (...) is clicked
private void ExecuteWriteFileDialog()
    {
        var dialog = new OpenFileDialog { InitialDirectory = _defaultPath };
        dialog.DefaultExt = ".txt";
        dialog.Filter = "TXT Files (*.txt)|*.txt";
        dialog.ShowDialog();
        FirmwarePath = dialog.FileName;

        WriteFileCommandExecuted();
    }

// Gets called when Write Button is clicked
public void WriteFileCommandExecuted()
    {
      // same logic as in c++
    }

如何在我的 WriteFileCommandExecuted() 方法中执行与 C++ 代码相同的操作?

请帮忙:)

最佳答案

//Inside void WriteFileCommandExecuted()

 System.IO.StreamReader sr = new System.IO.StreamReader("File Path");
                textBox1.Text =  sr.ReadToEnd();

        //Or if you need more control

        System.IO.FileStream fs = new System.IO.FileStream(FirmwarePath, System.IO.FileMode.CreateNew);
        System.IO.StreamReader sr = new System.IO.StreamReader(fs);
        string textdata = sr.ReadToEnd();
        int fileSize = (int)new System.IO.FileInfo(FirmwarePath).Length;

        Byte f_buffer = new Byte();
        f_buffer = Convert.ToByte(textdata);
        int cnt = 0;

    //The FirmwarePath is the path returned to you by your file dialog box.
    //If you want to write the class you will need to instantiate is System.IO.StreamWriter then //invoke the "write" method

关于c# - 在 WPF 中单击按钮执行文件操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12875945/

相关文章:

c# - 从 C# 调用 c 方法

c# - C# 中的 Diff b/w ref 和 out 参数

c++ - 如何将这些 OpenGL 着色器转换为适用于 Android NDK 的 GLES3 的 OpenGL ES 着色器?

C++ - 用于小数减法的 double 类型替代方案?

c# - StackExchange.Redis如何订阅多个 channel

c# - Web API 的 Lambda 表达式

c++ - if 子句中的自动赋值

.net - 从托管代码调用具有固定缓冲区的非托管 DLL(编码问题)

.net - OpenIDConnect .Net 框架中的 Multi-Tenancy

c# - 从后面的代码调用 javascript 方法