将 openFileDialog 设置为可变字符串

Setting openFileDialog to variable string

我正在尝试创建一个将重命名文件(最终是一批文件)的小型 GUI。我正在使用 C++ 和 Windows 用户(Visual Studio Community 2015)。

我有一个 btnSelectFiles 按钮,我想用它打开一个文件选择 GUI。

我正在尝试使用 openFileDialog 但很难将文件名设置为字符串变量。

我使用的代码:

public:
void btnSelectFiles_Click(Object^ /*sender*/, System::EventArgs^ /*e*/)
{
    IO::Stream^ myStream;
    OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;

    openFileDialog1->InitialDirectory = "c:\";
    openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    openFileDialog1->FilterIndex = 2;
    openFileDialog1->RestoreDirectory = true;

    if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
    {
        if ((myStream = openFileDialog1->OpenFile()) != nullptr)
        {
            // Insert code to read the stream here.
            myStream->Close();
        }
    }

    /*String test = openFileDialog1;*/
}

我的许多尝试之一是使用:

String test = openFileDialog1

我也试过:

String test = openFileDialog1.FileName 

但收到的表达式必须有 class 类型错误。

请有人帮我解决这个问题,从而帮助我理解这件事。我拿起的书没有涵盖这一点,我一直在努力寻找在线帮助。

由于您使用的是 C++/CLI(而不是 C++),因此您必须编写

String^ test = new String( openFileDialog1.FileName );