How to read a file directly without including in project appln? [Resolved]

Posted by Oswaldlily under ASP.NET on 3/1/2012 | Points: 10 | Views : 1606 | Status : [Member] | Replies : 8
I need to browse and read a file.
Can i do without adding this file in my project???
can i read it directly




Responses

Posted by: Oswaldlily on: 8/1/2012 [Member] Starter | Points: 25

Up
0
Down

Resolved
Using inputstream is correct method

Stream stread = fileupload.PostedFile.InputStream;
StreamReader reader = new StreamReader(stread);
char[] delimiters = new char[] { ',' };
string[] columns = reader.ReadLine().Split(delimiters);

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 3/1/2012 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
Use IO class to do that , as depicted below

try

{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("YouFilewithPath.txt"))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
response.write.(line);
}
}
}
catch (Exception ex)
{
//trap errors here
}
}


Thank you for posting at Dotnetfunda
[Administrator]

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/2/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi Vuyiswamb is correct, but to execute this code properly you need to upload your local file to server.
unless it's not possible.

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 3/2/2012 [Member] Starter | Points: 25

Up
0
Down
1)How to add to server thru code?

2)If added in server,whn i try to import 100 files,it is wastage of memory to be add 100 files in server.

so i tried to use File.delete(filename)

3)If above is the case,if From same server 2 or 3 people tryin to import same file,this delete function can disturb others who readin the same file..

so how to avoid all

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/2/2012 [Member] Bronze | Points: 25

Up
0
Down
Here you have to be careful while giving file name.
if possible then try to keep files seperation according to user.
so when more then one user try to access same file it could be managable for us.

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 3/2/2012 [Member] Starter | Points: 25

Up
0
Down
I used in this way
string path=File.ReadAllText(fileupload.FileName);

now my error is

Could not find file
'C\Program files\Common files\Microsoft Shared\Dev Server\10.0\filename.csv

my original path is C;/Desktop/folder/filename

what is this
C\Program files\Common files\Microsoft Shared\Dev Server\10.0?????????????????



Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/2/2012 [Member] Bronze | Points: 25

Up
0
Down
will you give me your code??

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Oswaldlily on: 3/2/2012 [Member] Starter | Points: 25

Up
0
Down
? string path=File.ReadAllText(fileupload.FileName);
String strConnection = "Data Source=Sourcename-30;Initial Catalog=catalogname;User ID=sa;Password=abc25;";
string fullPath = System.IO.Path.GetFullPath(fileupload.FileName);
string file = System.IO.Path.GetFileName(fullPath);
string dirPath = System.IO.Path.GetDirectoryName(fullPath);

string excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source= \"" + dirPath + "\\\";" + "Extended Properties=\"text;HDR=YES;FMT=Delimited\"";

OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
excelConnection.Open();
OleDbCommand cmd = new OleDbCommand("Select * from [" + fileupload.FileName +"]", excelConnection);
OleDbDataReader dReader = cmd.ExecuteReader();

Oswaldlily, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response