Am trying to read an excel file which is on shared drive, I knew the drive name (F:), but may not be the same for all the users who is using the program.So i have tried with the server name as below :
I knew to use Directory info and fileinfo to get the files list and filename but foremost, my problem is passing the file name in workbook.open() and to fetch it.
Kushi Bobby
Member
20 Points
29 Posts
Reading excel file which is on shared drive
Apr 03, 2012 04:42 AM|LINK
Hi,
Am trying to read an excel file which is on shared drive, I knew the drive name (F:), but may not be the same for all the users who is using the program.So i have tried with the server name as below :
filename = "\\\\servername\\foldername\\excel.xls"
if (!File.exists(filename)) {
workbooks.open(filename,0,.....)
....
Method is giving error, even the file is existing,
this one is working filename = "F:\\foldername\\excel.xls"
Can u pls advice, how to pass the file name in this open();
santosh.jagd...
Star
7625 Points
1454 Posts
Re: Reading excel file which is on shared drive
Apr 03, 2012 05:34 AM|LINK
try with Server.MapPath(@"MyRelativeDir\foldername\excel.xls")
plz check at http://stackoverflow.com/questions/3071193/system-invalidoperationexception-failed-to-map-the-path-shareddrive-public
MCP
Kushi Bobby
Member
20 Points
29 Posts
Re: Reading excel file which is on shared drive
Apr 03, 2012 06:04 AM|LINK
Hi Santosh,
Thanks for the reply.
But i am trying to access the file in shared drive not relative to the application root. So server.mappath isnt useful.
Tried with DirectoryInfo, but returns false.
Any advice?
santosh.jagd...
Star
7625 Points
1454 Posts
Re: Reading excel file which is on shared drive
Apr 03, 2012 06:18 AM|LINK
http://stackoverflow.com/questions/4661248/access-a-shared-folderwhich-is-protected
http://www.eggheadcafe.com/community/csharp/2/10116970/accessing-shared-folder-on-a-network-using-c-code.aspx
MCP
karthicks
All-Star
32146 Points
5530 Posts
Re: Reading excel file which is on shared drive
Apr 03, 2012 06:44 AM|LINK
hi, you can use Net Use Shell command, refer below code
Process.Start("net.exe", @"USE S: \\2.1.6.19\ToKarthick /user:domain1\user1 user1@d1").WaitForExit(); Response.Write(new DriveInfo("S").IsReady); Response.Write("</br>"); Response.Write(File.Exists(@"S:\Hello.txt"));Process.Start("net.exe", @"USE S: \\2.1.6.19\ToKarthick /user:domain1\user1 user1@d1").WaitForExit();
if (!File.exists("S:\ExcelData.xls")) {
workbooks.open(S:\ExcelData.xls,0,.....) ...
Karthick S
Kushi Bobby
Member
20 Points
29 Posts
Re: Reading excel file which is on shared drive
Apr 03, 2012 06:48 AM|LINK
Hi Santosh,
Thanks for the links.
I knew to use Directory info and fileinfo to get the files list and filename but foremost, my problem is passing the file name in workbook.open() and to fetch it.
Pls advice
Kushi Bobby
Member
20 Points
29 Posts
Re: Reading excel file which is on shared drive
Apr 19, 2012 02:57 AM|LINK
Hi Guys,
Thanks for the replies, but my problem is solved and its due to syntax error.
workbooks.open(filename,0,.....) is working fine, when declared filename as below:
filename = "\\\\servername\\foldername\\excel.xls"
need to mention 1 extra for each "\".
santosh.jagd...
Star
7625 Points
1454 Posts
Re: Reading excel file which is on shared drive
Apr 19, 2012 05:09 AM|LINK
filename = "\\\\servername\\foldername\\excel.xls" is the same as you have given in your 1st post.
then this should also work.
MCP
Kushi Bobby
Member
20 Points
29 Posts
Re: Reading excel file which is on shared drive
Apr 19, 2012 06:48 AM|LINK
Hi Santosh,
Yes it will work, But i have mistakenly coded as filename = "\\\servername\\foldername\\excel.xls" with one less "\" and ended with exception.
after using correct number of "\", i have succeded.
Thanks
santosh.jagd...
Star
7625 Points
1454 Posts
Re: Reading excel file which is on shared drive
Apr 19, 2012 06:50 AM|LINK
oh! that's good.
MCP