c# get path for directory in project - asp.net

I'm trying get the path of a folder in my project called EmailAttachments. I tried
File.Exists("~/EmailAttachments/TestReport.pdf")
but that returns false. How can I get the path to a directory in the program so I can write files to it and retrieve them later?
This is in asp.net, not winforms

If you're trying to get the ASP.NET local path, use Server.MapPath("~/EmailAttachments/TestReport.pdf") to get its fully qualified path.

Related

Qt - Auto detection of a file in application folder

I have a sqlite database of my Qt C++ application. Suppose I located it on my default build/release folder and I also placed the database file on that same folder. I have login.h and login.cpp. I want that the application may auto detect the database and open it. I will only provide the name of database (Ex: mydb.sqlite).
Database.addDatabase("QSQLITE");
Database.setDatabaseName("I will only provide database file name here.like: mydb.sqlite");
I want that the rest of the directory should automatically detected and the database connection works perfectly.
Suppose, I have the sqlite file in:
C:/Qt/build-myapp-mingw-32/mydb.sqlite.
I am taking a string variable called path. And I want that, the application automatically detect the whole path. And open the database connection.
You do not have to give the complete path to database to make it works.
You can refer to the path from your build dir or maybe use :
QCoreApplication::applicationDirPath();
To get the path of your app and then navigate through your directories with .. ?

How to get the project base path in Spring mvc web application?

I have spring mvc web application which is deployed on a tomcat server. On that project, I want to create folders and files in the runtime. currently I can create folders in the server root by using System.getProperty("catalina.base"). But when I deployed the project in a externel server I cant create folders on the root level of the tomcat server beacuse I don't have permissions. Intead of that I decided to create folders in following directory.
tomcat_dirctory/webapps/myproject_directory
so I need to create folders and files in side the myproject_directory in the runtime. Can anyone tell me what is the path for that. May be I can hardocde that as follows.
System.getProperty("catalina.base") + File.separator + "webapps"+File.separator+"myproject_directory"
But Instead of hard coding I prefer to know is there any alternative way to obtain my project path.
You can use getServletContext().getRealPath("/");
Gets the real path corresponding to the given virtual path.
For example, if path is equal to /index.html, this method will return the absolute file path on the server's filesystem to which a request of the form http://://index.html would be mapped, where corresponds to the context path of this ServletContext.
The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators.
Resources inside the /META-INF/resources directories of JAR files bundled in the application's /WEB-INF/lib directory must be considered only if the container has unpacked them from their containing JAR file, in which case the path to the unpacked location must be returned.
This method returns null if the servlet container is unable to translate the given virtual path to a real path.

Get Application File Path in Windows Forms Application

I have a simple windows form app that I need to get the file path for. I am placing a config file in the same directory and I need to be able to get the path to that file.
I have used
Application.CommonAppDataPath
but that returns the path with 1.0.0.0 at the end.
and
Applicaiton.StartupPath
but that returns the path with \bin\debug at the end
Is there anyway to get the path to just the main file directory without anything appended to the end?
Application.StartupPath is returning the path with \bin\debug on the end because that is where your code is running, at least on your development machine.
If you are going to deploy this away from your development machine then Application.StartupPath will give you what you're asking for - the file path for your application. And yes, if you have deployed the config file to that same location, your code is going to find it.
How to get the application also working on your development machine and get round the bin\debug issue? Well, a dirty hack would be just to chop the bin\debug string off the end of Application.StartupPath.
In that case, if you need to check for whether you're running inside the debugger, see this question
Try with
Dim aPath As String
Dim aName As String
aName = _
System.Reflection.Assembly.GetExecutingAssembly. _
GetModules()(0).FullyQualifiedName
aPath = System.IO.Path.GetDirectoryName(aName)
You can use
Application.ExecutablePath; which gives full path including executable file name
and
Application.StartupPath; for your current running application path directory without file name

SubSonic connection string for SQLite

I'm writing a desktop app which needs a simple persistence layer - I found out about SubSonic and it's capability to work with SQLite. However I need to keep the database file in user's AppData folder and don't know how to put such value into app.config - I don't want to use absolute paths.
Can app.config somehow access enviroment variables or reference application data folder?
For subsonic v2.x I would ignore the app.config connection string and just set it at runtime before working with the database. The provider name stays the same of course.
string dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), #"MyApplication\Northwind.db3");
DataService.Providers["Northwind"].DefaultConnectionString =
String.Format(#"Data Source={0};Version=3;New=False;Connection Timeout=3", dbPath);
There's no way to specify the AppData folder in the app.config for a connections string.
But what you could do is write the value to the config file either during install or when the application is first run.
The "framework way" of finding appdata is to use Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
This will find the correct appdata path regardless of platform.
There are several ways if you are using ASP.NET , in either language
Server.MapPath("~") will return the root of the application as a full path name then you can just add "/app_data" to it to get you're full path.
Alternatively inspect the HttpContext.Current.Request and HttpContext.Current.Application
there are numerous ( and much better then the one I just mentioned ) properties that will provide you with the same folder - being the root of the application as s full path.
Note that these should all work even if you have the application as a virtual folder and a regular folder with an application configures in IIS on that folder
However this is only possible at runtime , so it can't really be mentioned in the app.config. you could try using relaltive paths from where the app.config is resident IE "../App_Data" or "/App_data" but I'm not sure of you're exact requirements.
Good luck

Load XmlTextReader from a file in my web app via a method in another DLL

I am creating a method in a DLL that is going to be dropped into a web application.
The web application has a folder with a XML file in it:
/files/myfile.xml
My dll has to reference that DLL, but I can't hard code the path to the file obviously.
How can I reference that file and load a XmlTextReader with that file?
The best thing would be to have the path to the XML file be stored in a configuration file. Since both of your assemblies will be running in the same AppDomain they will both have access to the setting in the configuration file and you don't have to worry about hard-coding the path.
The simplest way that comes to mind would be to pass a filename parameter to the DLL's method from the web application. I'm sure you've already thought of this, but your question doesn't say what (if any) problems exist with that solution.

Resources