如何将 C++ SaveFileDiolog 信息转换为 LPCTSTR

How to convert C++ SaveFileDiolog info to LPCTSTR

我一直在开发 C++ 应用程序..我想出了如何从 SaveFileDialog 中获取目录名称并将其与文本结合以将一堆文件保存到同一个文件夹中,但代码结束了当我尝试将我的新 FileWithPathName 转换为 LPCTSTR 时未分配。

我搜索了整个网站,但似乎无法找到一个非常清楚的例子来说明我要找的东西。如果有人可以指导我找到对此很清楚的 link 或者告诉我我做错了什么,那就太好了。 ;-)

            FileInfo^ fi = gcnew FileInfo(saveFileDialog1->FileName);

            String^ fileNameWithPath = gcnew String(fi->DirectoryName) + "newName.txt";

            //LPCWSTR lfileNameWithPath = (LPCWSTR)(pfileNameWithPath[0]);  // get temporary LPSTR // fails to get initialized
            //LPCTSTR lfileNameWithPath = (LPCTSTR)(Marshal::StringToHGlobalAnsi(fileNameWithPath)).ToPointer(); // data returned like Chinese characters. epic fail

有几种不同的方法可以进行这种转换。您可以使用:

#include <msclr/marshal.h>
using namespace msclr::interop;
using namespace System;

String^ fileNameWithPath = gcnew String(fi->DirectoryName) + "newName.txt";

marshal_context context;
LPCTSTR lfileNameWithPath = context.marshal_as<LPCTSTR>(fileNameWithPath);

更多here