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?

huysentruitw
  • 27,376
  • 9
  • 90
  • 133
  • 2
    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? – Frédéric Dec 30 '15 at 16:03
  • @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? –  Dec 30 '15 at 16:19
  • You can use this project as an [Example](http://tbf.me/a/Bwi3Ap) attached in the [post](http://stackoverflow.com/questions/34517107). I am doing same as he did but it is pretty much same. –  Dec 30 '15 at 16:22

3 Answers3

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.

Community
  • 1
  • 1
Frédéric
  • 9,364
  • 3
  • 62
  • 112
  • 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 it –  Dec 30 '15 at 16:44
  • 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 questions –  Dec 30 '15 at 16:45
  • 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. – Frédéric Dec 30 '15 at 16:50
  • 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. –  Dec 30 '15 at 16:54
  • 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 diescription –  Dec 30 '15 at 17:36
  • @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`. – Frédéric Dec 30 '15 at 18:00
  • Or you may combine the path with `..\..\\` to get back to root project folder, but it still looks as a bad design to me. – Frédéric Dec 30 '15 at 18:02
  • `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";` –  Dec 30 '15 at 22:19
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.

user1413
  • 527
  • 4
  • 21
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.

Community
  • 1
  • 1
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164