Make .txt file unreadable / uneditable

前端 未结 10 1786
北海茫月
北海茫月 2020-12-29 03:29

I have a program which saves a little .txt file with a highscore in it:

 // Create a file to write to. 
string createHighscore = _higscore + Environment.NewL         


        
10条回答
  •  没有蜡笔的小新
    2020-12-29 03:47

    Here is a code to make an text file not editable. in the same way you use this technique to make it not readable etc.

    string pathfile = @"C:\Users\Public\Documents\Filepath.txt";
    
    if (File.Exists(pathfile))
    {
        File.Delete(pathfile);
    }
    if (!File.Exists(pathfile))
    {
        using (FileStream fs = File.Create(pathfile))
        {
            Byte[] info = new UTF8Encoding(true).GetBytes("your text to be written to the file place here");
    
            FileSecurity fsec = File.GetAccessControl(pathfile);
            fsec.AddAccessRule(new FileSystemAccessRule("Everyone",
            FileSystemRights.WriteData, AccessControlType.Deny));
            File.SetAccessControl(pathfile, fsec);
        }
    }
    

提交回复
热议问题