Change temporary path - asp.net

Exsits any way to modify the temporary folder path returned by System.IO.Path.GetTempPath() method?
My asp.net application run under iis 7.
Thanks

Presumably, you don't want to change the returned path (value), but change the actual path?
As you can see here, the value returned depends on a number of possible environmental variables. You'd need to change these to change the returned path.

I wonder why you want to change that, as no matter what path it is returned, your application uses it as a temp folder (where the files can be cleaned freely).
If your application does care the existence of the files, write them to your own temp folder, which means you should give up System.IO.Path.GetTempPath(). Many applications, even Microsoft's, use their own temp folders.
I don't think you should change any temp folder environment variables as that will affect other applications unnecessarily (though they should not care if they really treat the folder as a temp folder :)).

Related

Copy Just the Folder

I wanted to Copy just a single folder at a time from one location to another
Currently i hv to First go to the rename copy the name and come back to current location and create a new folder and paste the name
Just like TreeCopy Which copies folder complete structure
is there anything through which i can speed up this process and just generate a single folder from a 3000+ Collection of Folders
How can i copy just one folder from one location to another without copying anything inside it neither content nor sub folders.
if you know the way let me know.
I know how hectic that work can get, but luckily there's an easy way around this.
Type in the following command in command prompt(cmd).
I think you need to have administrator rights for this.
xcopy “c:\users\cdwyer\documents\OriginalFolder” “c:\users\cdwyer\documents\NewFolder” /T /E
The command is xcopy and it takes two parameters, the path of original folder and the path of new folder you want to create with the original folder structure.
/T is just another parameter that ensures to only copy the folder structure, not the files.
/E is another parameter you can include to make sure empty folders are created.
If you have any other queries, just go through this website.
https://camerondwyer.com/2013/07/05/how-to-copy-an-entire-folder-structure-without-copying-the-files-tip-for-starting-the-new-financial-year/

When people mention storing files on the File System, is that referring to a folder within the app?

I need to store pdf files that will keep increasing with time on a folder for my app. But I'm not sure which method is consider 'good practice' between if I should store it in a folder within the project architecture (i.e. inside Content folder) or in a folder outside the app. I tried searching online but most people just mention 'File System' and don't give examples on where do they store it.
So my question is, when people mention storing files on the File System, do they usually mean storing it in a folder inside the app (like Content folder) or do they mean storing it in a folder outside the app?
For example: let's say my apps folders/files are within the container folder: /Container/AppFolder/. Should I store the pdf files on the apps Content folder (i.e. /Container/AppFolder/Content) or should I create a new folder OUTSIDE the apps folders to hold these pdf files? (i.e. /Container/PDFFiles, so basically two folders within the container folder - one folder for the pdf files and the other folder for the app)? Which one is consider a good practice? I have considered just putting these files on the Content folder but as the amount of files keep increasing I'm not sure if is bad practice.
Thanks in advance.
You can put it where ever you want on the server, the program will just need proper permissions to that location (I use the IO namespace). However you should put thought into how to organize the files and folders. Is each file to be tied to a specific ID or just all in 1 folder? Think of things you'll need to handle like invalid file names (special characters), duplicate file names etc. Is the location strictly 1 way (upload only)? Or are you enabling download functionality? If you have download functionality stress security, probably creating a download.aspx page specifically for handling download requests and authentication.

File Path and Root issues

So I have my the path to my website code as follows:
C:/folder1/folder2/folder3/my published website code from VS2012 - on my website I get an attachment and I want to save it to the following path C:/folder4
when I try the following code: file.SaveAs(Server.MapPath("../../folder4/") + filename); it says that I am going past the root. Can someone explain to me what is going on and if and how I can solve this issue?
Server.MapPath() is used to get the path in relation to the server root. Since your trying to save it outside the server virtual directory, you could probably just hardcode the file.
file.SaveAs(#"C:/folder4/" + filename);
It might not work depending on your IIS worker pool permissions.
file.SaveAs(Server.MapPath("folder4/") + filename);
Because I cannot see your folders structure I would reccomend setting a breakpoint after Server.MapPath() to see the full URI Path to determin your next steps since it says you are past root you may have one to many "../" before your string.
As per the documentation for HttpServerUtility.MapPath:
you cannot specify a path outside of the Web application
which is exactly what you are trying to do. If you interpret "the root" to be the root folder of your application, that is even what the error message is telling you.
Either
use an absolute path or
store your data beneath the application folder
use MapPath("~/") to get the current directory and build a relative path from that (in essence, you just move the "../.." outside the call to MapPath)
I would probably recommend going with 2. as it will give less headaches wrt. permissions and multiple sites hosted on the same server.
Server.MapPath(...) tries to return a physical ("real") directory for the virtual or relative path you give it. And since a virtual directory can't be located "over" the root in that sense, what you're trying to do makes no sense. You can go from domain.com/somefolder to domain.com/, but you can't really go any farther back.
You could instead use Environment.CurrentDirectoryas the starting point to find your folder, and apart from that just use SaveAs(..) as you're already doing.

Creating a new file without using a ServletContext

Assume I want to write to a new file created within the space of my webapp.
One way would be use getServletContext().getRealPath("/") and use that String to create a new file on the server. However, often I come across advice like not using getServletContext().getRealPath("/").
Can someone please let me know of another way to write a new file within my webapp?
Many thanks.
Have some configuration property containing the absolute path of a directory outside of the webapp and web server path, read this path from the configuration property, and write to this directory.
To serve files from this directory, write a servlet that takes the name or ID of this file as parameter, reads the file from the directory, and sends its content to the response.
This will
work even if the app is deployed as a war file and never unzipped to the file system
allow you to redeploy the next version of the app or server without deleting all the uploaded/created files
allow you to add whatever control you want on the uploaded/created files, instead of making them available to everyone
In short, treat these files as data, stored in a database which happens to be the file system instead of a SQL database.

How to get exact path of servlet where it is running?

I have an application that I use in a servlet. The application assumes the text database residing in the same directory where it is being executed. When I am trying to use it in servlet and even after placing the text database files in /WebContent, /DataProject and also src folders. The application cannot find the database. I need to know exactly where the servlet file is being executed so I can place the database files in the same directory. I have already /.metadata/.plugin......../tmp0/wtpwebapps directory. Any help will be greatly appreciated.
Usually when we access files in java we give the absolute path. Dont use the relative path. We use relative paths for jsp/html/css etc. For accessing normal files use the complete path. So put the files in /home/tomcat/.../../directoryDatabase

Resources