Get FilePath for my excel file with sheetname
Asked Answered
F

3

6

I am trying to do is to get filepath for my excel file. But I am unable to do so.

File is in Document/Visual Studio 2013/Project/ProjectName/a.xlsx

string path = Path.Combine(HttpContext.Current.Server.MapPath("~/"),"a.xlsx");
string SheetName="Sheet1";

Is it wrong way to do it or is it correct way?

Flaw answered 30/12, 2015 at 15:49 Comment(3)
Your question lacks details in my opinion. May you tell us: 1 - Document/Visual Studio 2013/Project/ProjectName is the root of a web projet; 2 - your code snippet is executed in the context of this web project responding to a http request; 3 - what is the code which does fail? Presumably something trying to open path, but it is lacking in your code snippet; 4 - have you tried to inspect path local variable value?Milinda
@Frederic 1. Yes it is the root of my project but it is not a web project. 2. I am just calling to the path of my file 3. The code which fails is in snippet 4. What are you trying to say in Question 4?Flaw
You can use this project as an Example attached in the post. I am doing same as he did but it is pretty much same.Flaw
M
1

This is the better answer according to me.

Better to save in

C:\Users\AJ1110\Documents\Visual Studio 2013\Projects\Proj\Proj 

And in

program.cs

string pathfile = @"..\..\a.xlsx";
string sheetName = "Whatever_SheetName_IS!!!";

This might solve your problem.

Monadelphous answered 30/12, 2015 at 22:35 Comment(0)
T
1

HttpContext.Current does not work outside of a web context.

If your project is running inside a console or windows program, it cannot work with HttpContext.Current. MapPath is meant to translate a web path to a file system path. ~/ is a .Net convention for pointing the root web path of a web application.

You should explicit what are your requirements about how to resolve the folder containing your file.

Maybe should you simply put that in some configuration file (using settings property tab of the project by example) and retrieve it from there.

Edit:

So, from your comment on this question, it looks like you have to seek the xl file in the executing folder.

There is a number of ways for achieving this, depending on your application use cases.

By example, check this question.

Thornburg answered 30/12, 2015 at 16:34 Comment(8)
1. I am pretty much getting what are your trying to say. Let's take this as a scenario if I have to different machine wherever I copy this project I have to change this again and again. So that is why I trying to get dir path and continue with itFlaw
2. What do you mean by using settings property tab? Hope you are understanding what I am trying to do here. Let me know If you or anyone have any questionsFlaw
2. from project properties (right click on project in solution explorer), choose its settings tab. It will ask for adding a settings file and corresponding code files for accessing settings. But if your requirement is to search the file in the executing folder, this would not help you much. I am going to edit this answer for adding some more help.Milinda
It basically does not search the file. well it does sort of and then it opens it. That why I added as an Example in my previous comment to show some what I am trying to do in my project. Again appreciate you all for helping and making me understand.Flaw
Tried using var thisPath = System.IO.Directory.GetCurrentDirectory(); as per your example but it goes all the way to debug folder. And in GetDirectoryRoot it does to C: No option to get the file path the way I have explained in my diescriptionFlaw
@AJ1110 If you try to get the file in your source folder, something looks wrong to me. A program is not supposed to be deployed with its sources usually. In solution explorer, if your xl file is included in projet, select it then go to its properties and set Copy to Output Directory to if newer or always.Milinda
Or you may combine the path with `..\..\` to get back to root project folder, but it still looks as a bad design to me.Milinda
C:\Users\AJ1110\Documents\Visual Studio 2013\Projects\Proj in this is my xl file. And I also tried with Path.Combine --> string pathfile = Path.Combine("../../", "Data.xlsx"); string sheetName = "Login";Flaw
M
1

This is the better answer according to me.

Better to save in

C:\Users\AJ1110\Documents\Visual Studio 2013\Projects\Proj\Proj 

And in

program.cs

string pathfile = @"..\..\a.xlsx";
string sheetName = "Whatever_SheetName_IS!!!";

This might solve your problem.

Monadelphous answered 30/12, 2015 at 22:35 Comment(0)
A
0

Since your project is not a Web one, I expect that you some sort of Output where build process generates an executable file, some assemblies etc. You can put Build action of your Excel as Content (more details here) and use this base path to retrieve it:

System.Reflection.Assembly.GetExecutingAssembly().Location

It is important to think in terms relative to your executable (or executing assembly to be more precise), since your output will have to run outside your development environment and your excel must still be accessible.

Also, getting the exact executing assembly might be tricky in some scenarios.

Allowance answered 30/12, 2015 at 23:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.