0

Hello I'm very new to coding but I developed a tool which works for local use. My Boss wants me to put it on our website for customers. I figured out ClickOnce is a way to do this.

The .exe is reading a .csv file to find data for use:

public string FindSalesmenPhone(string userName)
        {
            List<string> resLines = new List<string>();
            var lines = File.ReadLines(@"S:\data.csv");
            foreach (var line in lines)
            {

                var res = line.Split(new char[] { ',' });

                //id to search 
                if (res[7] == userName)
                {
                    resLines.Add(res[10]);
                }

            }
            //to get the output  
            foreach (var line in resLines)
            {

                return line;
            }

            MessageBox.Show("no phone found!");
            return null;
        }

My question is: How can I change this path and will the .csv-file still be accessible after I deployed the tool with ClickOnce.

List<string> resLines = new List<string>();
            var lines = File.ReadLines(@"S:\Manuel\data.csv");

Can I simply change it to something like:

var lines = File.ReadLines(@"http://mywebsite.com/data.csv");

Sorry might be easy-pie for you guys but I really appreciate your help!

EisBaer
  • 33
  • 1
  • 5
  • Why not just have people use Excel? It imports CSV files no problem. Even those downloaded from the web. Why are you building a tool to do something everyone can already do? – Jasmine Feb 03 '15 at 21:10
  • This is just a small part of the code... There are way more functions Excel could not provide. – EisBaer Feb 03 '15 at 21:44

2 Answers2

0

It would not be File.ReadLines as that is using File.IO and you're asking to get the text from a webpage. The best way to go about this would be to create a WebRequest to the URL you have specified and then read the csv from there using a StreamReader. See this thread which is similar.

EDIT:

Glad this helped you. You may have some bad performance simply because you called the StreamReader without a using statement. Try the code below:

public string TestCSV(string id)
    {

    List<string> splitted = new List<string>();
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://mywebsite.com/data.csv");
    HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

    using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
    {
    string currentline = sr.ReadLine();
    if (currentline != string.IsNullOrWhiteSpace)
    {

        var res = currentline.Split(new char[] { ',' });

        if (res[0] == id)
        {
            splitted.Add(res[1]);
        }

        foreach (var line in splitted)
        {

            return line;
        } 
     }
  }
 return null;
}
Community
  • 1
  • 1
Ryan C
  • 572
  • 5
  • 18
0

Thanks Ryan, this was very helpful.

I modified it for my use, but now the performance is very bad. Guess the Tool reads the file over and over again...(A few points call this method during runtime)

Is there a way I can save the .csv in a 2D Array and then save this for the whole runtime of the program? The .csv contains about 900 entries..

public string TestCSV(string id)
    {

        List<string> splitted = new List<string>();
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://mywebsite.com/data.csv");
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

        StreamReader sr = new StreamReader(resp.GetResponseStream());

        string currentline;
        while ((currentline = sr.ReadLine()) != null)
        {

            var res = currentline.Split(new char[] { ',' });

            if (res[0] == id)
            {
                splitted.Add(res[1]);
            }

            foreach (var line in splitted)
            {

                return line;
            } 
        }
        return null;
    }
EisBaer
  • 33
  • 1
  • 5