【问题标题】:Opening a file to StreamReader with string.format C#使用 string.format C# 将文件打开到 StreamReader
【发布时间】:2018-04-09 14:43:18
【问题描述】:

我认为我在这里是个白痴,但我是 C# 新手。如果我运行以下

 StreamReader sr = new StreamReader(@"./common/hostname");
 string line= sr.ReadLine();
 sr.Close();

代码执行得很好,但是如果我运行以下代码

// lstboxSites.Text == foo
StreamReader sr = new StreamReader(string.Format(@"./site/{0}", lstboxSites.Text));
string line = sr.ReadLine();
sr.close()

这会引发“路径中的非法字符”的错误,如果我尝试输入目录本身它会起作用

StreamReader sr = new StreamReader(@"./site/foo");
string line = sr.ReadLine();
sr.close()

我是个白痴吗?如果我将中间部分输出到 messagebox.show() 那么它是正确的

理想的工作示例——已编辑

            // user clicks on list box item, read value on click
        StreamReader sr = new StreamReader(string.Format(@"./site/{0}", lstboxSites.Text));
        string line = sr.ReadLine();
        sr.Close();
        // write to a text box
        txtFakeBox.Text = line;

【问题讨论】:

  • 你想达到什么目的?第二个代码块对我来说毫无意义
  • lstboxSites 是什么?
  • 嘿。基本上我需要它,所以当用户单击“列表框”中的项目时,它将读取文件并将其显示到“文本框”中。
  • 使用string text = File.ReadAllText(myPath)。尝试使用\ 而不是/。并检查 . 是不是你认为的那样
  • 要调试这样的东西,我会把string.Format放在单独的行上并设置一个断点,看看你得到的字符串是否是你所期望的,然后在new StreamReader(myString)中使用它

标签: c#


【解决方案1】:

如果它是一个列表框,那么您可能想从中获取一个项目。

var selectedItem = lstboxSites.SelectedItem;
StreamReader sr = new StreamReader(string.Format(@"./site/{0}", selectedItem ?? ""));
var line = sr.ReadLine();
sr.close();

那么你还需要真正检查文件是否存在

// Get selected item
var selectedItem = lstboxSites.SelectedItem;

// If user has not selected anything...
if (selectedItem == null)
{
    // Do whatever in this situation
}

// Get full path
var filePath = string.Format(@"./site/{0}", selectedItem ?? "");

// If file doesn't exist...
if (!File.Exists(filePath))
{
    // Do whatever in this situation
}

StreamReader sr = new StreamReader(filePath);
var line = sr.ReadLine();
sr.close();

【讨论】:

  • 您不小心尝试编辑其他人的帖子而不是您自己的帖子。请注意,您永远不必为自己的帖子提出修改建议,因此,如果网站说您要建议 修改而不是立即应用它,那么您就是在修改其他人的作品。
  • 感谢提醒,我会记住的
【解决方案2】:

如果它是一个列表框,那么您可能想从中获取一个项目。

StreamReader sr = new StreamReader(string.Format(@"./site/{0}", lstboxSites.Items[0].ToString()));
string line = sr.ReadLine();
sr.close()

【讨论】:

  • 我在编辑中添加了理想的工作示例,列表的项目将根据用户单击的选项而有所不同,希望对您有所帮助
  • 我一开始打字就按了发送并发布了大约 4 行代码。我的答案现已发布,但需要同行评审才能出现
  • 这个可以删除,我重新发布为一个新的答案,因为这个似乎卡在“审查”中。现在有人可以删除这个副本吗?
【解决方案3】:

在网上搜索了几个小时后,我发现了以下帖子“How to remove new line characters from a string?

所以要修改Angelisix的代码

if (!File.Exists(filePath))
{
    MessageBox.Show($"File \"{filePath}\" not found");
}
else
{
    filePath = filePath.Replace("\n", String.Empty);
    filePath = filePath.Replace("\r", String.Empty);
    filePath = filePath.Replace("\t", String.Empty);
    StreamReader sr = new StreamReader(filePath);
    string line = sr.ReadLine();
    sr.Close();
    txtFakeBox.Text = line;
}

Messagebow.Show() 似乎没有附加 /n... 很抱歉浪费了时间,我已经习惯了 Python 命令行应用程序 :) 并感谢所有 cmets

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-28
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    相关资源
    最近更新 更多