如何在 UWP 应用域外部创建文件?

Roy Li - MSFT 32,466 信誉分 Microsoft 供应商
2024-04-23T06:22:54.0533333+00:00

你好

我添加了broadFileSystemAccess。现在,我想检查文件是否存在。如果它不存在,我想创建文件。

文件路径如下所示:C:\a\b\c\test.log

谢谢  

此问题由How to create file outside the UWP app domain? - Microsoft Q&A总结而来.

通用 Windows 平台 (UWP)
通用 Windows 平台 (UWP)
一个 Microsoft 平台,用于生成和发布适用于 Windows 桌面设备的应用。
25 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Junjie Zhu - MSFT 15,601 信誉分 Microsoft 供应商
    2024-04-24T03:52:39.84+00:00

    你好,

    可以使用StorageFolder.TryGetItemAsync去检查文件是否存在。

    如果文件不存在,可以使用StorageFolder.CreateFileAsync创建文件。

        StorageFolder desktopfolder = await StorageFolder.GetFolderFromPathAsync("C:\\a\\b\\c");
        var checkfile = await desktopfolder.TryGetItemAsync("test.log");
        if (checkfile == null)
        {
            Debug.WriteLine("not exist");
            StorageFile newfile = await desktopfolder.CreateFileAsync("test.log", CreationCollisionOption.ReplaceExisting);
        } 
        else
        {
            Debug.WriteLine("already exist");
        }
    
    
    

    谢谢.


    如果答案是正确的解决方案,请点击“接受答案”并投赞成票。如果您对此答案有其他疑问,请点击“评论”。 注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。

    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助