In my ASP.NET MVC website, I have to read a txt file with some names and emails separeted by ';'. After that, I have to save each line of this txt file to database.
Googling around, I've found some snippets, but in all of them I have to use the txt file path.
But, how can I get this path? This file could be anywhere in a user's machine!
Thanks!!
the form in the HTML should have
enctype="mulitipart/form-data"
If you are on a asp.net web page model then
Server.MapPath("~/")
works to get the root of the site so pass the path you need. You might need to callFor instance a folder where the text files are saved:
To just read from it once you have it you can StreamReader it:
If this is for a desktop application then the Applcation class has all this information:
http://msdn.microsoft.com/en-us/library/system.windows.forms.application.startuppath.aspx
All properties list other appdata folders and stuff but once you have the application path to the executable this gives you context such as
Application.LocalUserAppDataPath
.http://msdn.microsoft.com/en-us/library/system.windows.forms.application_properties.aspx
If the content is small enough you could also just save them in a
HashTable
or a GenericList<String>
before saving to the database as well.You cannot get the full path of an uploaded file. This would be a privacy violation for the user that uploaded the file.
Instead, you'll need to read the Request.Files that have been uploaded. For example: