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.
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
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!
tdb
Member
116 Points
284 Posts
line count of uploaded file in vs2005
Oct 07, 2009 07:08 AM|LINK
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.
booler
Star
14342 Points
2205 Posts
Re: line count of uploaded file in vs2005
Oct 07, 2009 09:17 AM|LINK
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.
tdb
Member
116 Points
284 Posts
Re: line count of uploaded file in vs2005
Oct 07, 2009 11:49 AM|LINK
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?
kesava_pindi
Member
32 Points
19 Posts
Re: line count of uploaded file in vs2005
Oct 07, 2009 11:55 AM|LINK
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);
booler
Star
14342 Points
2205 Posts
Re: line count of uploaded file in vs2005
Oct 07, 2009 11:59 AM|LINK
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!
tdb
Member
116 Points
284 Posts
Re: line count of uploaded file in vs2005
Oct 08, 2009 04:54 AM|LINK
Hi
I did that also..
no use
aresssriniva...
Contributor
3518 Points
838 Posts
Re: line count of uploaded file in vs2005
Oct 08, 2009 05:08 AM|LINK
Hi,
Try this
Server.MapPath("~/Files/" + filen1)
Srinivas Ramanujan
Words offer the means to meaning, and for those who will listen, the enunciation of truth !!!!!
tdb
Member
116 Points
284 Posts
Re: line count of uploaded file in vs2005
Oct 08, 2009 09:19 AM|LINK
Thank You all so much
now it works with both:-
Server.MapPath("Files/"+filen1)
and
Server.MapPath("~/Files/"+filen1)