問題描述
我正在創建一個電子應用程序,我需要在其中掃描和更新需要 root 權限的文件.我知道我可以以這種方式使用 sudo.exec() 運行此類命令:
I am creating an electron app where I need to scan and update files which need root permission. I know I can run such commands using sudo.exec() in that way:
sudo.exec ("rm /private/var/log/fsck_hfs.log", options, (e, stdout, stderr) => {});
甚至我可以在一個腳本中放入多個命令并使用單個 sudo.exec() 執行它們.就我而言,命令需要在不同的時間點執行,我無法使用單個腳本執行它們.用戶一次又一次地授予權限很煩人.
And even I can put multiple commands in a script and execute them with single sudo.exec(). In my case, commands needs to be executed at different point of time and I cannot execute them with a single script. It is annoying for user to grant permissions again and again.
我嘗試了另一種方式(運行一段代碼,可以執行所有需要 root 權限的內容).為此,我在這里發布了另一個問題.但似乎不可能.
I tried to do it another way (to run a piece of code where everything that require root permission can be executed). For that, I posted another question here. But it seems that it is not possible.
現在我想要一種在使用安裝應用程序時獲得許可的方法(就像大多數應用程序在用戶安裝它們之后所做的那樣)并且能夠在整個應用程序中使用 sudo.exec() 或其他一些方法來執行命令(最好還有需要root權限的代碼fs.readdir等).
Now I want a way to get permission once when use install app (as most of the apps does after user install them) and be able to use throughout the app sudo.exec() or some other method to execute commands (preferably also the code fs.readdir, etc) that require root permission.
推薦答案
您需要以管理員權限運行應用程序.如果您使用 electron builder 構建應用程序,請使用 requestedExecutionLevel 值作為requireAdministrator"
You need to run the applicaton with administrator privilege. If you are using electron builder to build the app, use the requestedExecutionLevel value as "requireAdministrator"
https://www.electron.build/configuration/win
這篇關于如何在電子應用程序中獲取持久權限?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!