【问题标题】:How to read dconf settings using C#如何使用 C# 读取 dconf 设置
【发布时间】:2014-04-02 13:37:59
【问题描述】:

我正在使用 MonoDevelop 开发一个 ubuntu 统一应用程序。

如何使用 C# 读取 dconf 设置?

我可以使用 C# 访问 GSettings 吗?

我真正需要的是键/org/gnome/desktop/interface/font-name的值。

更新

我的快速破解如下:

private string GetFontName()
{
    string font = "Ubuntu 11";

    Process p = new Process {
        StartInfo = new ProcessStartInfo {
            FileName = "gsettings",
            Arguments = "get org.gnome.desktop.interface font-name",
            UseShellExecute = false,
            RedirectStandardOutput = true,
            CreateNoWindow = true
        }
    };

    p.Start();

    while (!p.StandardOutput.EndOfStream)
        font = p.StandardOutput.ReadLine();

    p.WaitForExit();

    return font.Trim(new char[]{'\''});
}

【问题讨论】:

  • 不应该绑定到 GSettings 吗?
  • @rene。你说的对。如here 所述:“大多数应用程序不想直接与 dconf 交互,而是与 GSettings 交互。
  • 如果有影响,您可以使用GLib.Settings 类(类似于 GSettings API),它是 GTK#3 中gio-sharp.dll 的一部分。 GTK#3 为我打破了一些东西,这就是为什么我会坚持你的 hack。

标签: c# mono gtk gnome


【解决方案1】:

我从不使用 Ubuntu 和 Mono。 我认为下面的 ode 可能会对您有所帮助。

using System;
using System.IO;

namespace FileAccess
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            string FileName="TestFile.txt";

            // Open the file into a StreamReader
            StreamReader file = File.OpenText(FileName);
            // Read the file into a string
            string s = file.ReadToEnd();
            // Close the file so it can be accessed again.
            file.Close();

            // Add a line to the text
            s  += "A new line.\n";

            // Hook a write to the text file.
            StreamWriter writer = new StreamWriter(FileName);
            // Rewrite the entire value of s to the file
            writer.Write(s);

            // Add a single line
            writer.WriteLine("Add a single line.");

            // Close the writer
            writer.Close();
        }
    }
}

【讨论】:

  • 好吧,如果 dconf 文件的格式是文本,这可以工作。这些文件不是简单的文本文件,因此此答案不适用于 OP。
猜你喜欢
  • 1970-01-01
  • 2010-12-30
  • 1970-01-01
  • 2011-07-25
  • 2013-08-01
  • 2010-10-26
  • 2010-10-17
  • 2016-11-06
  • 1970-01-01
相关资源
最近更新 更多