使用文件

发布于 2024-08-10 17:54:47 字数 516 浏览 7 评论 0原文

我需要创建一个读取文件的应用程序,如下所示:

Font = "Courier New"
Size = "10"
Style = "Bold"

如您所见,我将使用它来设置 TextBox 的一些属性,但我将存储每个内容在这些引号内并将它们存储在一些变量中(textFonttextSizetextStyle)。

我认为我可以使用这种语法来打开带有文件的流:

StreamReader reader = new StreamReader("confs.txt");

问题是:

  • 当声明 StreamReader 时,它将读取应用程序目录中的文件?(只是为了确认)
  • 我怎样才能只读取引号内的内容并将每个存储在一个字符串中?

我认为,文件 I/O 在 Windows Mobile 和 Windows 中是相同的。此致

I'm needing to create a application that will read a file, that will be like this:

Font = "Courier New"
Size = "10"
Style = "Bold"

As you can see I'm going to use this to set some properties of a TextBox, but I'm going to store each thing that is inside these quotes and store they on some variables(textFont, textSize, textStyle).

I think that I can use this syntax to open a stream with the file:

StreamReader reader = new StreamReader("confs.txt");

The questions are:

  • As the StreamReader is declared it will read the file in the application directory?(Just to Confirm)
  • How can I read only what is inside the quotes and store each one in a string?

As I think, file I/O is iqual in Windows Mobile and Windows. Best Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

腻橙味 2024-08-17 17:54:48

像这样的事情怎么样:

foreach(string s in FileLines) {
  string[] data = s.Split(new[] { '=' });
  // property is in data[0]
  // value is in data[1]
}

您创建的是一个简单的 CSV 文件

,其中 CSV 我的意思是字符分隔值...

更新

好的,看起来您正在尝试精确复制而不是理解算法,所以我将在这里创建一个新的

StreamReader reader = new StreamReader("confs.txt");
while(reader.peek >= 0) {
   string l = reader.ReadLine();
   string[] data = l.Split(new[] { '=' });
   // property is in data[0]
   // value is in data[1]
}
reader.Close();
reader.Dispose();

How about something like this:

foreach(string s in FileLines) {
  string[] data = s.Split(new[] { '=' });
  // property is in data[0]
  // value is in data[1]
}

What you have created is a simple CSV file

Where by CSV I mean Character Separated Value...

Update

Ok, seems like you are trying to copy exactly instead of understanding the algorithm so I will create a new one here

StreamReader reader = new StreamReader("confs.txt");
while(reader.peek >= 0) {
   string l = reader.ReadLine();
   string[] data = l.Split(new[] { '=' });
   // property is in data[0]
   // value is in data[1]
}
reader.Close();
reader.Dispose();
瘫痪情歌 2024-08-17 17:54:48

最好声明完整路径,或使用以下语法:

System.IO.Path.Combine((new System.Uri(Assembly.GetEntryAssembly().CodeBase)).AbsolutePath, "confs.txt");

我认为解析它,您可以更轻松地将文件保存为 Xml 并将其加载到 XmlDocument 中。或者更好的是,放弃这个想法并将这些信息存储在 app.config 文件中。它非常容易,如果需要的话,您仍然可以在记事本中编辑它,而无需重新编译。

(这会使答案的前半部分变得毫无意义)

It's better to declare the full path, or use the following syntax:

System.IO.Path.Combine((new System.Uri(Assembly.GetEntryAssembly().CodeBase)).AbsolutePath, "confs.txt");

I think to parse it, you would have an easier time saving the file as Xml and loading it into an XmlDocument. Or better yet, abandon this idea and store tine information in the app.config file. It's so much easier and you still get to edit it in notepad without recompiling if you need to.

(which would render the first half of the answer as moot)

凹づ凸ル 2024-08-17 17:54:48

1:只要不改变当前目录就可以。还有更彻底的方法来获取应用程序目录,不过

有两种:解析此类字符串的任意多种方法; string.SplitRegex等;纯粹作为说明,我将(ab)使用DbConnectionStringBuilder

using (StreamReader reader = File.OpenText("data.txt")) {
    string line;
    DbConnectionStringBuilder db = new DbConnectionStringBuilder();
    while ((line = reader.ReadLine()) != null) {
        db.ConnectionString = line;
        foreach (string key in db.Keys) {
            Console.WriteLine(key);
            Console.WriteLine(db[key]);
        }
    }
}

编辑DbConnectionStringBuilder可能不存在于windows-mobile中;也许只是 = 上的 Split 或使用 Regex;它归结为精确格式。

1: as long as you don't change the current directory, yes. There are more thorough ways to get the app directory, though

2: any number ways to parse such a string; string.Split, Regex, etc; purely as an illustration, I'll (ab)use DbConnectionStringBuilder:

using (StreamReader reader = File.OpenText("data.txt")) {
    string line;
    DbConnectionStringBuilder db = new DbConnectionStringBuilder();
    while ((line = reader.ReadLine()) != null) {
        db.ConnectionString = line;
        foreach (string key in db.Keys) {
            Console.WriteLine(key);
            Console.WriteLine(db[key]);
        }
    }
}

edit DbConnectionStringBuilder may not exist in windows-mobile; perhaps just Split on = or use Regex; it comes down to the exact format.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文