首页 文章

C / winRT CreateFolderAsync - 如何检查现有文件夹?

提问于
浏览
-2

我正在尝试更新文件夹中的文件 . 首先,我需要检查文件夹是否存在,如果不存在,则在该文件夹中创建一个空文件 . 如果文件夹不存在,则使用FailIfExists或OpenIfExists的CreationCollisionOption的try / catch不起作用 .

其他选项:GenerateUniqueName和ReplaceExisting不合适 . 代码:............ //获取存储文件夹StorageFolder _storageFolder = ApplicationData :: Current() . LocalFolder();

//get one of it's sub folders
StorageFolder _turboCalc = nullptr; //no default constructor
bool _folderFound = false;
try {
    _turboCalc = co_await _storageFolder.CreateFolderAsync(L"TurboCalc", CreationCollisionOption::OpenIfExists); //create sub folder in sub folde
      //_turboCalc = co_await _storageFolder.CreateFolderAsync(L"TurboCalc", CreationCollisionOption::FailIfExists); //create sub folder in sub folde
}
catch (winrt::hresult_error const& ex) {
    _folderFound = false;
}
StorageFile  _fileDoubles = nullptr; //no default constructor
if (!_folderFound) { //creae the folder and an empty file
    _turboCalc = co_await _storageFolder.CreateFolderAsync(L"TurboCalc");
    _fileDoubles = co_await _turboCalc.CreateFileAsync(L"FileDoubles.dbo", CreationCollisionOption::ReplaceExisting); //create file in sub folder
}

1 回答

  • -1

    以下是它导致问题的原因:该示例的目标是更新文件,这意味着访问不在方法中但通过参数传递的数据..我创建了一个没有任何外部数据的测试方法 - 现在try / catch工作 . 工作是一个黑客!它要求调用者始终串联调用两个方法!第一个没有外部数据,但有try / catch,确保文件和文件夹存在 . 第二个没有尝试/捕获 . 我想我会继续使用C#直到这个问题得到解决

相关问题