0

I think im being an idiot here, but im new to C#. If i run the following

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

the code executes fine, but if i run the following

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

this raises an error for "Illegal characters in path", if i try and type in the directory itself it works

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

Have i been an idiot ? if i output the middle section to messagebox.show() then its correct

Ideal working example -- edited

            // 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;
Alistair Bendall
  • 538
  • 4
  • 10
  • What are u tryin to achieve ? The 2nd code-block makes no sense to me – Software Dev Apr 07 '18 at 12:57
  • `lstboxSites` is a what? – ProgrammingLlama Apr 07 '18 at 13:00
  • Hey. Basically i need it so when a user clicks on an item in a "List Box", it will read the file and display it into a "Text Box". – Alistair Bendall Apr 07 '18 at 13:01
  • Use `string text = File.ReadAllText(myPath)`. Try to use `\ ` instead of `/`. And check that `.` is what you think it is – Kalten Apr 07 '18 at 13:04
  • Try lstboxSites.SelectedItem.ToString(). To use the the Text property as you are doing, you must have ListBox.SelectionMode = MultiExtended. [RTM](https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.text(v=vs.110).aspx) – RamblinRose Apr 07 '18 at 13:08
  • Do not try to build paths via String Connaction. Use the static Path class to get the proper path for your current OS (https://msdn.microsoft.com/en-us/library/system.io.path.aspx). Or otherwise extract them from Windows (SpecialFoldersEnumeration). – Christopher Apr 07 '18 at 13:11
  • Kalten - ive been playing with / and \ for ages now, defiantly in the right spot with ./ . RamblinRose made no difference it still throws an error. Christopher can you suggest another option for the ideal working solution ? – Alistair Bendall Apr 07 '18 at 13:32
  • Try something like `txtFakeBox.Text = Path.GetFullPath(string.Format(Path@"./site/{0}", lstboxSites.Text));` before trying to read the content. That should help you validate the path. – Kalten Apr 07 '18 at 13:39
  • 3
    To debug something like this, I would put the `string.Format` on a separate line and set a breakpoint, to see if the string you get is what you expect, before using it in `new StreamReader(myString)` – JamesFaix Apr 07 '18 at 15:51

3 Answers3

0

If it's a listbox then you probably want to get an item from it.

StreamReader sr = new StreamReader(string.Format(@"./site/{0}", lstboxSites.Items[0].ToString()));
string line = sr.ReadLine();
sr.close()
BSMP
  • 4,596
  • 8
  • 33
  • 44
Mirko Acimovic
  • 508
  • 2
  • 9
  • I added the ideal working example to an edit, the Item of the list will vary depending on which option the user clicks on, hope that helps – Alistair Bendall Apr 07 '18 at 13:08
  • I pressed send as soon as I started typing and posted about 4 lines of code. My answer is now in but needs peer reviewing to appear – angelsix Apr 07 '18 at 15:29
  • This can be deleted, I re-posted as a new answer as this one seems to be stuck in "review". Anyone able to delete this duplicate now? – angelsix Apr 07 '18 at 16:01
0

If its a listbox then you probably want to get an item from it.

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

Then you still need to really check if the file exists with

// 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();
angelsix
  • 392
  • 1
  • 7
  • You accidentally tried to edit someone else's post instead of your own. Note that you'll never have to suggest an edit for your own posts so if the site is saying you're about to *suggest* an edit instead of apply it immediately, you're editing someone else's work. – BSMP Apr 07 '18 at 23:40
  • Thanks for the heads up I'll remember that – angelsix Apr 08 '18 at 06:46
0

after searching the net for hours i found the following post "How to remove new line characters from a string?"

So to modify Angelisix's code

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;
}

It appears the Messagebow.Show() wasn't appending the /n... Sorry to have wasted time, i'm used to Python command line applications :) and thanks to all the comments

Alistair Bendall
  • 538
  • 4
  • 10