【问题标题】:SHGetSpecialFolderPath to a System::String^SHGetSpecialFolderPath to a System::String^
【发布时间】:2012-06-21 15:55:41
【问题描述】:

我正在尝试将 SHGetSpecialFolderPath 解析为字符串,准确地说是 System::String^
我现在正在使用此代码:

TCHAR appDataPath;
SHGetSpecialFolderPath(0, appDataPath, CSIDL_APPDATA, false);

我已经尝试过这样的事情,但它也不起作用:

LPWSTR appDataPath;
SHGetSpecialFolderPath(0, appDataPath, CSIDL_APPDATA, false);

我只想得到这样的System::String^

System::String ^ loc_inst = appDataPath + "\\inst\\info.xml";

【问题讨论】:

  • 使用 Environment::GetFolderPath() 代替。

标签: string visual-c++ c++-cli appdata tchar


【解决方案1】:

我不认为 C++/CLI 可以自动连接 char 数组并将它们分配给字符串句柄。我认为您需要像这样实例化一个新的System::String 对象:

System::String^ loc_inst = gcnew System::String(appDataPath);
loc_inst.Append("\\inst\\info.xml");

或者你可以使用StringBuilder,但如果你想创建一个新的String对象,我认为你必须使用gcnew和一个构造函数。

请记住,appDataPath 不是String,而是您之前分配的字符数组。但是,System::String 允许您在其构造函数之一中传入 char 数组。

【讨论】:

  • 啊,谢谢,现在编码:StringBuilder^ build_str = gcnew StringBuilder("", MAX_PATH); for (int i = 0; i Append(gcnew array{appDataPath[i]}); } System::String^ loc_inst = build_str->ToString();
猜你喜欢
  • 2022-12-27
  • 1970-01-01
  • 2022-12-02
  • 2022-12-02
  • 2022-12-02
  • 2022-12-01
  • 1970-01-01
  • 2022-12-02
  • 2022-12-19
相关资源
最近更新 更多