中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
如何设置文件的数据等级?
微信扫码分享
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%') } }