Click here to Skip to main content
15,741,692 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a file that contains ids and image path separated by |
1|~/images/img1.jpg
2|~/images/img2.jpg
--------------------
I want to pass the id, so it searches and returns the path of the image
i dont want to use database,,
it doesnt return the path ,, it only return null whuy ??
i am passing an exisiting id though


C#
<pre lang="cs">public string GetImagePath(string id)
      {
          const string f2 = "C:\\idimg.txt";
          string myline;
          using (StreamReader r = new StreamReader(f2))
          {
              // 3
              // Use while != null pattern for loop


              while ((myline = r.ReadLine()) != null)
              {

                  string[] attr = myline.Split('|');
                  string myidimage = attr[0].ToString();
                  string pathimage = attr[1];
                  if (id == myidimage)
                  {
                      myline = pathimage;
                      return myline;
                  }
                  return null;

              }
          }
Posted

C#
<pre lang="cs">public string GetImagePath(string id)
      {
          const string f2 = "C:\\idimg.txt";
          string myline;
          using (StreamReader r = new StreamReader(f2))
          {
              // 3
              // Use while != null pattern for loop


              while ((myline = r.ReadLine()) != null)
              {

                  string[] attr = myline.Split('|');
                  string myidimage = attr[0].ToString().Trim();
                  string pathimage = attr[1];
                  if (id == myidimage)
                  {
                      myline = pathimage;
                      return myline;
                  }
                  return null;

              }
          }


Trim() spaces whenever you read a string from textfile...
 
Share this answer
 
Your code looks OK, maybe you have an error in the file.
You use string comparison, so make sure you do not have spaces at beginning or end of 'id' or 'myidimage'.
Try using:
id.Equals(myidimage, StringComparison.InvariantCultureIgnoreCase)
instead of id == myidimage

also you don't need .ToString() on attr[0] - it already is a string.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900