line count of uploaded file in vs2005

Last post 10-08-2009 5:19 AM by tdb. 7 replies.

Sort Posts:

  • line count of uploaded file in vs2005

    10-07-2009, 3:08 AM
    • Member
      86 point Member
    • tdb
    • Member since 04-03-2009, 7:48 AM
    • Posts 218

    I want to count the lines in a file. I can count the lines wen a filename is statically written and file is placed in the some drive.

    Wat I want is to "upload any file whose lines are to be counted". The person should be able to upload any file,then its count should be displayed.

    It takes c:\prog...\somee path\myfiletest.txt and say file doesnt exists. Though I hv saved it to the folder in my project itself.

    How to to do it?

    Please help.

  • Re: line count of uploaded file in vs2005

    10-07-2009, 5:17 AM
    • Star
      14,342 point Star
    • booler
    • Member since 08-15-2005, 2:22 PM
    • Brighton, England
    • Posts 2,205

    You could do something like this:


                    int lines = 0;
                    using (FileStream fs = File.Open("file_path", System.IO.FileMode.Open, 
                        System.IO.FileAccess.Read, System.IO.FileShare.Read))
                    {
                        using (StreamReader reader = new StreamReader(fs))
                        {
                            while (reader.ReadLine() != null)
                            {
                                lines++;
                            }
                        }
                    }
    
                    Response.Write(string.Format("There are {0} lines.", lines));


    If the file is within a virtual directory in your application, then your code should be able to read it. If it's not finding it, check the path you have supplied is correct.


  • Re: line count of uploaded file in vs2005

    10-07-2009, 7:49 AM
    • Member
      86 point Member
    • tdb
    • Member since 04-03-2009, 7:48 AM
    • Posts 218

    Hi

    Thank u but I hv already done that:-

    protected void Button1_Click(object sender, EventArgs e)
        {
            string filen1 = "";
            if (FileUpload1.HasFile)
            {
                try
                {
                    filen1 = FileUpload1.FileName;
                    FileUpload1.SaveAs(Server.MapPath("Files/" + filen1));
                }
                catch (Exception err)
                {
                    lblMsg.Visible = true;
                    lblMsg.Text = err.Message;
                }
            }
            else
            {
                lblMsg.Visible = true;
                lblMsg.Text = "Please upload the file!!";
            }
            long g = CountLines(filen1);
            //long g = CountLines("D:/test.txt");
            Response.Write(g);     
        }
    
        static long CountLines(string f)
        {
            long count = 0;
            using (StreamReader r = new StreamReader(f))
            {
                string line;
                while ((line = r.ReadLine()) != null)
                {
                    count++;
                }
            }
            return count;
        }

    My Problem is the path-when I have uploaded the file, it goes to files folder in my project, but when same path is passed to countlines function it takes something as :-

    c:\programfiles\.....\files\myfile.txt

    it cant find it,Its obvious my file is in c:\inetpub\wwwroot\myproject\files\myfile.txt

    I dont know how to rectify the path problem?
     

  • Re: line count of uploaded file in vs2005

    10-07-2009, 7:55 AM
    Answer
    • Member
      30 point Member
    • kesava_pindi
    • Member since 09-16-2009, 12:05 PM
    • Posts 14

    hi

    in ur counntlines method u passed only the name of the file insted of that pass the path of the fiel

    for ex:server.mappath("/files/"+filename);


  • Re: line count of uploaded file in vs2005

    10-07-2009, 7:59 AM
    • Star
      14,342 point Star
    • booler
    • Member since 08-15-2005, 2:22 PM
    • Brighton, England
    • Posts 2,205

    You should ensure that the ASP.NET worker process has read permissions on the Files subdirectory. It should be granted these automatically if the directory is within your project, but sometimes the directory permissions can get in a mess.

    One easy way to check if the problem is permissions-related is to allow 'Everyone' read access to the subdirectory using the security tab on the directory properties in windows explorer. If this fixes the problem, then you know it is a permissions thing- but remember to remove this setting afterwards!

  • Re: line count of uploaded file in vs2005

    10-08-2009, 12:54 AM
    • Member
      86 point Member
    • tdb
    • Member since 04-03-2009, 7:48 AM
    • Posts 218

    kesava_pindi:

    hi

    in ur counntlines method u passed only the name of the file insted of that pass the path of the fiel

    for ex:server.mappath("/files/"+filename);


     

    Hi

    I did that also..

    no use

  • Re: line count of uploaded file in vs2005

    10-08-2009, 1:08 AM
    Answer
    • Contributor
      2,242 point Contributor
    • aresssrinivas
    • Member since 10-20-2007, 4:45 AM
    • Nasik
    • Posts 570

    Hi,

      Try this

            Server.MapPath("~/Files/" + filen1)


    Regards,
    Srinivas Ramanujan

    Words offer the means to meaning, and for those who will listen, the enunciation of truth !!!!!
  • Re: line count of uploaded file in vs2005

    10-08-2009, 5:19 AM
    • Member
      86 point Member
    • tdb
    • Member since 04-03-2009, 7:48 AM
    • Posts 218

    Thank You all so much

    now it works with both:-

    Server.MapPath("Files/"+filen1)

    and

    Server.MapPath("~/Files/"+filen1)

Page 1 of 1 (8 items)