Products
Database Search Solution (New Version) Search Control SEO Pager Highlighter Shortcut Controls Crypt Package Free ASP.NET Controls
Geotargeting Component ASP.NET Media Player Control Flash Video Player Control Services
ASP.NET Telecommute Jobs Free IP Location Lookup Test .Net Regular Expressions CSS/Table/DIV Page Layouts Custom Programming Article Sites Master List |
Mainpulating Files and Directories in ASP.NETLet suppose, you have to enable your website users to upload their pictures. Or suppose you have to place functionality of uploading product images along with the product description. In this article, we will explain how you can provide such functionalities. We may need to manipulate files and directories on our web server in different ways. For example, applications need to create a new file, delete an existing file, modify the contents of an existing file, and check whether a file with given name exists or not. Similarly, directories are also required to be manipulated. ASP.Net gives us the opportunity to handle such operations using the namespace System.IO. Important Classes for File and Directory ManipulationHere are some of the important classes in the namespace System.IO:
Before proceeding further, it's important to note that you need to make sure that appropriate security permissions are set on folders where you are manipulating your files and directories. When you are doing it on your local development machine, you can set these permissions in this way:
When your site is live and you are doing it on hosting server, you need to know how your hosting service handles security settings. This procedure varies in different hosters. Some hosters may have provided you with some tool, which you can use to configure security settings on your folders. For example, WebHostForASP.NET provides its own Control Panel to handle all custom settings. Others may do it with some third party tool like Helm or Plesk. How to Write a File on Web Server
Include System.IO name space with line private
void WriteFile() Execution of this code results in creation of a text file named "test.txt" in the path specified which contains this text: Sample Text in Line 1 You may need to test whether a file with same name on same path already exists or not. You can do so using Exists property of FileInfo class.
private
void WriteFile() You may need to add some more text in an already existing file. AppendText() method of FileInfo class is used to achieve it.
string
filePath = "C:\\Inetpub\\wwwroot\\test.txt"; Now, text in the file "test.txt" takes this form: Sample Text in Line 1 Extension property can be used to find the extension of FileInfo object. string extension = objFileInfo.Extension(); Manipulating Directories in ASP.NETHere are few methods and properties which can be used to manipulate directories: CreateDirectory method takes the string parameter which tells the directory path to create. CreateSubDirectory method is used by a DirectoryInfo object to create a directory within the directory represented by calling DirectoryInfo object. Parent property of DirectoryInfo object can be used to return another object of DirectoryInfo representing parent of calling object.
//To Create a New Directory A practical example of file and directory manipulation in ASP.NETLet's consider a practical example where we need manipulation of files and directories. You may have a website where your users can register. Along with asking them for other necessary information, you may want them to have an opportunity to upload their profile image or images demonstrating their business activities. There are two approaches which can be used to save an image or any other file. One approach involves asking the user to give path of image they want to save. It gets the file path, makes a FileInfo object of this path, converts it into binary coding and saves it in database. A field of varbinary datatype is used to hold such data. Later on, when this image is required to retrieve, same binary code is fetched and converted into an image file. This approach is usually adopted when it's required to upload few files or images, because it involves database operations. The other approach has nothing to do with database operations. It involves IO operations instead. You can save the files uploaded by users on server space. This is usually adopted when it's required to upload too many files on server. For example, a social networking site having too many users needs to upload multiple images by each user. In this approach, one important issue is to have the path where you can save the image user uploaded. A FileInfo or DirectoryInfo object doesn't take URL of the path where you have to save image. URL can be used only, if you are going to "get" file path from server. To create a directory in which you are going to upload the file, you need to give path information in this way: string dipath = System.Web.HttpContext.Current.Server.MapPath("") + "\\UserImages\\" + Session["LoggedUserId"].ToString(); To achieve this, you need to have a file upload control and a button on your aspx page.
<asp:FileUpload
ID="EditFileUpload"
runat="server"
Width="289px"
/> When user chooses an image file by clicking "Browse", its path is displayed in EditFileUpload control. Then user clicks "Save", btnSaveImage_Click method is called, which was defined in the code behind file. We are going to follow a procedure where we put images uploaded by a user in a folder which is named by that user's ID. This user folder will be uploaded in a folder "UserImages" which we already created in our space. In other words, images uploaded by a user whose ID is 241 will have this path: http://www.yourdomain.com/UserImages/241/Image1.jpg protected
void btnSaveImage_Click(object
sender, EventArgs e) To retrieve this image back, in order to display it on a web page, you just need an Image Control <asp:Image ID="Image1" runat="server" Height="171px" Width="195px" /> To set its ImageUrl property, you need to retrieve it from database. Suppose, you get your desired results in a dataset named dsImages and it has a column "ImagePath" which contains the path of image you want to display. Image1.ImageUrl = dsImages.Tables[0].Rows[0]["ImagePath"].ToString(); Where "ImagePath" may contain a value like this: http://www.yourdomain.com/UserImages/241/Image1.jpg If you want to avoid writing such code in code behind, you can use another way to assign retrieved ImagePath value to ImageUrl Property of the image control. Here is the sample code: <asp:Image ID="Image1" runat="server" ImageUrl='<%# Bind("ImagePath") %>' Height="171px" Width="195px" /> This is code of aspx page, where Image control is defined. Every time a dataset is bound with this image, value in "ImagePath" column of that dataset table will be assigned to ImageUrl property of this image control. Summary • Make sure that working directory has got full control for ASPNET user. Tutorial toolbar: Tell A Friend | Add to favorites | Feedback | |