5

In an Visual Studio environment, Project A (ASP.NET) references Project B (C#) in my solution like this:

Solution
├─Project B
│ ├─data.txt
│ └─process.cs  (a class BB with static initialization new FileStream("data.txt"))
└ Project A (referencces Project B)
  ├─bin
  │ └─data.txt (copied from project B each time project B changes)
  └─Controllers
    └─mycontroller.cs (references the class BB)

The problem is that when I run the application, the working directory is C:\Program Files (x86)\IIS Express\, so that data.txt cannot be read from the compiled version of process.cs which is in bin.

For the moment, to solve the problem, I manually copied data.txt to this folder, but this solution is not viable. Note that changes to B must be coherent with other projects depending on B, which are not all ASP.NET project.

What changes should I make so that data.txt is accessible from my project without relying on me to copy the data.txt file to the IIS Express directory?

I would like to port my program to Azure Online and I cannot rely on this method. Thank you for your help.

Other linked answers:

  • This answer is ASP.NET - specific, I cannot add server.MapPath because the project does not know about it.
  • This answer references project paths, but does not give me an hint about how to modify them.
Community
  • 1
  • 1
Mikaël Mayer
  • 10,425
  • 6
  • 64
  • 101
  • And why you believe you cannot use Server.MapPath?! Also what makes you think the problem describe is specific only for Azure?! Your problem is indeed ASP.NET specific! And there is nothing in Azure that is different for Your scenario. – astaykov Sep 16 '14 at 09:55
  • Indeed and I accept your modification, although it gives a perspective and an explanation why I need to solve this path. If I was not publishing my work somewhere else, I could just make a script to copy the file in the IIS directory. – Mikaël Mayer Sep 16 '14 at 10:11

4 Answers4

2

"Note that changes to B must be coherent with other projects depending on B, which are not all ASP.NET project"

Not sure what coherent would mean to you in context of other projects which depend on project B... But following options come to mind -

  1. Embed data.txt as a resource in the assembly generated for project B. Project B, can then read the file as a resource (This assumes, file contents do not need to be modified after the build) See ResourceManager class for handling resources embedded in assemblies. http://msdn.microsoft.com/en-us/library/system.resources.resourcemanager(v=vs.110).aspx

  2. If it works for all clients of Project B, scan sub-directories for the file.. This is more hackish.. but really depends on the scenarios for project B.

Vikas Gupta
  • 4,455
  • 1
  • 20
  • 40
  • I did not use Resources, but Assembly (and I had to stay within the same class). See http://stackoverflow.com/a/2041057/1287856 – Mikaël Mayer Sep 17 '14 at 19:51
2

Ensure that data.txt is being deployed somewhere. Then, make your library take the path to it or the base directory path as an argument. The caller of the library is the application which has concrete knowledge of how to obtain the file's path.

Or, embed the .txt into the DLL as a manged resource.

usr
  • 168,620
  • 35
  • 240
  • 369
0

To me your problem is not about copying the file - but its location. A shared file should be kept in a shared / central location accessible by all applications. In your current situation this could be (and this isn't exhaustive)...

  • Database Blob
  • Explicit File Location
  • Online, retrieved over HTTP
  • Online, retrieved over FTP
  • Networked location
  • Running a small TCPIP server between the programs so that one acts as the source and child processes request the file.

In respect to porting your application to Azure, Azure provides a BlobStorage along with functionality to access the file in an unsecured and secured manner (see Shared Access Signatures).

HTH

SeanCocteau
  • 1,838
  • 19
  • 25
0

Can't you just configure your project B with your project A ? I mean giving the path of your txt file to some static field of a configuration class from project A to project B.

elpaulo
  • 86
  • 4