#鸿蒙学习大百科#如何设置文件的数据等级?

如何设置文件的数据等级?

HarmonyOS
2024-10-22 15:53:43
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
后知后觉cy
import securityLabel from '@ohos.file.securityLabel';
import { BusinessError } from '@ohos.base';
import common from '@ohos.app.ability.common';
import fs from '@ohos.file.fs';

@Entry
@Component
struct Index {
  //获取UIAbilityContext
  private context = getContext(this) as common.UIAbilityContext;
  build() {
    Row() {
      Column() {
        Text("设置文件的数据等级")
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            // 获取需要设备数据等级的文件沙箱路径
            let context = getContext(this) as common.UIAbilityContext; // 获取UIAbilityContext信息
            let pathDir = context.filesDir;
            let filePath = pathDir + '/test.txt';

            //打开文件
            let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
            // 设置文件的数据等级为s0
            securityLabel.setSecurityLabel(filePath, 's0').then(() => {
              console.info('Succeeded in setSecurityLabeling.');
              fs.closeSync(file);
            }).catch((err: BusinessError) => {
              console.error(`Failed to setSecurityLabel. Code: ${err.code}, message: ${err.message}`);
            });
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
分享
微博
QQ
微信
回复
2024-10-22 22:29:47
相关问题
#鸿蒙学习大百科#什么是用户文件
1091浏览 • 0回复 待解决