首页 > 解决方案 > 在最终设置中读取属性时出现问题

问题描述

我做了一个程序,它需要一封邮件来开始扫描 PC 中的元素,比如处理器的名称、速度、内存、默认浏览器等。

问题是,在我的本地调试中,程序运行正常,所以我用输出项目制作了一个安装文件,但是,当我运行它时,它给我带来了权限错误,避免这种情况的唯一方法,它正在运行程序作为管理员,但在我所做的其他设置中,永远不要给我那个错误。

这是我的方法:

// Creates the Json to save usermail
public string createMailPropertie(string newEmail)
{
    string newRoute = "";

    try
    {
        var jsonObject = new JObject();
        jsonObject.Add("userEmail", newEmail);

        var json = JsonConvert.SerializeObject(jsonObject);

        File.WriteAllText(Directory.GetCurrentDirectory() + @"/userMailPropertie.json", jsonObject.ToString());

        // write JSON directly to a file
        using (StreamWriter file = File.CreateText(Directory.GetCurrentDirectory() + @"/userMailPropertie.json"))
        using (JsonTextWriter writer = new JsonTextWriter(file))
        {
            jsonObject.WriteTo(writer);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
            
    return newRoute;
}

我想知道是否有其他方法可以让它更清洁、更实用。

标签: c#visual-studio-2019system.io.file

解决方案


推荐阅读