user2376998
user2376998

Reputation: 1071

Connection string data source path to access files from other projects

i have 2 projects , 1 is a web application and another is WPF , in my web application i have a upload page to allow users to upload their files i used Server.MapPath(@"~/something/") for the directory but how do i retrieve that file in my WPF application , i can retrieve by typing the full path C:/programfiles/projects ... etc , but i am retrieving the directory from the database and in the database it reads like that ~/something/filename.jpg .

I only save ~/something/filename.jpg in the database , How do i retrieve the full path without manually typing the full path?

the Full path is in another project like this C:/programfiles/projects/visualstudio/projectname/something/filename.jpg.

How to i access the file with only ~/something/filename.jpg in another project? there is an error saying path does not exists unless i type the full path in but i don't want that .

Some say we have to add a domain directory in the connectionstring of my App.Config then later i need to use it and combined it with the database url to get my full path . But i got no idea how to do that .

connectionstring :

  <add name="istellarEntities2" connectionString="metadata=res://*/ModelSQL.iStellar.csdl|res://*/ModelSQL.iStellar.ssdl|res://*/ModelSQL.iStellar.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=IMAC-PC10\SQLEXPRESS;initial catalog=istellar;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

How i retrieve from my database ( the URL , using entity framework daoWordPuzzle is a class file which consists of CRUD codes ) :

@daoWordPuzzle.GetfileURL() // however this displays only ~/something/something.jpg

I need to have the full path . How do i do that?

Upvotes: 1

Views: 2258

Answers (1)

Arsen
Arsen

Reputation: 965

Firstly, a quick question: Is your WPF application running on the WEB SERVER that also runs the website?

Or is your WPF app on a desktop and web server is somewhere else?

In the first case: You can simply store the first part of the path in appSettings of your WPF application to tell you where the directory is. For example and store only the last part of the path in the database column. This way you can use Path.Combine() from System.IO namespace to find the location.

The same approach can be used in the other case, but it depends on if you have UNC share to the web server directory that stores the files. If not, your WPF app may need to access the files via HTTP using a URL instead of file path. If yes, you can simply store the path prefix in the app config like so "\WEBSERVER\Something"

using System.Configuration;

string fileUploadDirectory = ConfigurationManager.AppSettings["FileUploadDirectory"];

// using Substring(1) to skip the ~ that is stored in the database and is returned by GetfileURL()
string fullFilePath = Path.Combine(fileUploadDirectory ,daoWordPuzzle.GetfileURL().Substring(1)) 

Upvotes: 3

Related Questions