读取Kotlin / Native

时间:2019-03-05 10:04:50

标签: kotlin environment-variables kotlin-native

好,所以我想创建一个本机应用程序以读取一些环境变量,并根据一些业务逻辑将它们的值更改为其他值。我决定在科特林做这件事。

通过创建函数,我设法找到了如何更改系统环境变量的方法:

fun call(arg: String) = platform.posix.system(command)

fun setEnvironmentVariable(variable: String, value: String) {
    println("Changing value of $variable to $value")
    call("SETX $variable $value")
}

但是它只会更改值。我想将其存储在Kotlin变量中并对其进行一些操作。

发出语句call(“ SET $ variable”)将值打印到命令提示符,但是我无法弄清楚如何从Kotlin捕获它。我猜是否有一种方法可以将命令提示符中的输出提取到String中,这样即使Windows以myvariable = myvalue的形式(而不是仅以值的形式)将其输出,也可以使它变得更容易。

预先感谢

1 个答案:

答案 0 :(得分:2)

以下是在Kotlin / Native中读取环境变量的方法:

import platform.posix.*
import kotlinx.cinterop.*

fun main() {
    val variable = "whatever..."
    println(getenv(variable)?.toKString())
}