Embed
Email

Computer Notes

Document Sample
Computer Notes
Stats
views:
73
posted:
8/19/2009
language:
English
pages:
11
Questions and answers in this FAQ have been collected from newsgroup posts, various mailing lists and the employees of Syncfusion. 15. Files 15.1 What is the best way to rename a file on the webserver in code? 15.2 15.3 15.4 15.5 15.6 15.7 15.8 15.9 How to create a folder in ASP.NET? How to show the ASP.NET code to the users? How to read a html file in ASP.NET? How can I to get the path to the system area that holds temporary files? How to save a file in the client machine? How to get the physical path of a file? How to get the current filename? How to Upload files in ASP.NET? FAQ Home



15.10 How to delete a file from the server? 15.11 How to find the date and time the specified file or directory was last written to? 15.12 How to get the File information using ASP.NET? 15.13 How to create a .csv file that grabs the data from the database? 15.14 How to read text file in ASP.NET? 15.15 How to read specific characters from a text file? 15.16 How to check files exist in a particular directory? 15.17 What is a MemoryStream and how to use MemoryStream in ASP.NET? 15.18 How to detect if the string indicating a file-system resource is a file or directory?



15.1 What is the best way to rename a file on the webserver in code? Use namespace System.IO VB.NET



File.Move("C:\Dir1\SomeFile.txt", "C:\Dir1\RenamedFileName.txt")



C#



File.Move(@"C:\Dir1\SomeFile.txt", @"C:\Dir1\RenamedFileName.txt")



Refer Rename Function FileInfo.MoveTo Method Note: In a Web application, the code is running in the context of the machine\ASPNET account, which has limited privileges. If the error you are getting pertains to permissions, you might need to grant to the machine\ASPNET account the rights to create and delete files in the directory where you're working. Note that this could be a security issue. 15.2 How to create a folder in ASP.NET? Use System.IO namespace VB.NET



Dim path As String = "" try ' Determine whether the directory exists. If Directory.Exists(path) Then Response.Write("That path exists already.") Return End If ' Try to create the directory. Dim di As DirectoryInfo = Directory.CreateDirectory(path) Response.Write(("Directory create successfully at " + Directory.GetCreationTime(path))) catch ex as Exception Response.Write (ex.Message ) end try



C#



string path = @"c:\MyDir"; try { // Determine whether the directory exists. if (Directory.Exists(path)) { Response.Write ("That path exists already."); return; } // Try to create the directory. DirectoryInfo di = Directory.CreateDirectory(path); Response.Write("Directory create successfully at " + Directory.GetCreationTime(path)); } catch(Exception ex) { Response.Write (ex.Message ); }



15.3 How to show the ASP.NET code to the users? Use namespace System.IO System.Text



VB.NET



Protected Function GetCode(filename As String) As String Dim sr As New StreamReader(filename) Dim sb As New StringBuilder() sb.Append("") sb.Append(sr.ReadToEnd()) sb.Append("") sr.Close() Return sb.ToString() sb = Nothing sr = Nothing End Function 'GetCode Private Sub Button1_Click(sender As Object, e As System.EventArgs) Response.Write(GetCode((Server.MapPath("WebForm1.aspx") + ".vb"))) End Sub 'Button1_Click



C#



protected string GetCode(string filename) { StreamReader sr =new StreamReader(filename ); StringBuilder sb =new StringBuilder(); sb.Append(""); sb.Append(sr.ReadToEnd()); sb.Append(""); sr.Close(); return sb.ToString(); sb = null; sr = null; } private void Button1_Click(object sender, System.EventArgs e) { Response.Write (GetCode(Server.MapPath ("WebForm1.aspx") + ".cs")); }



15.4 How to read a html file in ASP.NET? Use namespace System.IO VB.NET



Dim file As String = Server.MapPath("temp.html") Dim sr As StreamReader Dim fi As New FileInfo(file) Dim input As String = "" If File.Exists(file) Then sr = File.OpenText(file) input += Server.HtmlEncode(sr.ReadToEnd()) sr.Close() End If input += "" Me.Label1.Text = input



C#



string file = Server.MapPath ("temp.html"); StreamReader sr; FileInfo fi = new FileInfo(file); string input = ""; if(File.Exists(file)) { sr = File.OpenText(file); input += Server.HtmlEncode(sr.ReadToEnd()); sr.Close(); } input += ""; this.Label1.Text = input;



15.5 How can I to get the path to the system area that holds temporary files? Use System.IO namespace VB.NET



Dim filePath As String = Path.GetTempPath() Dim fileName As String = Path.GetTempFileName() Response.Write((filePath + " ")) Response.Write(fileName)



C#



string filePath =Path.GetTempPath (); string fileName = Path.GetTempFileName(); Response.Write (filePath + " " ); Response.Write (fileName );



15.6 How to save a file in the client machine? The browser will not allow you to save a file directly to a client machine. You could however do a Response.Redirect("http://server/filename"); which would send the file back to the browser, at which point the user would be prompted to save / open the file.



15.7 How to get the physical path of a file? Use Request.Path



15.8 How to get the current filename? VB.NET



Response.Write (Path.GetFileName(Request.PhysicalPath))



C#



Response.Write (Path.GetFileName(Request.PhysicalPath));



15.9 How to Upload files in ASP.NET? Take a look at following articles VB.NET code C# Code



15.10 How to delete a file from the server?



VB.NET



System.IO.File.Delete(Server.MapPath("wnew.txt"))



C#



System.IO.File.Delete(Server.MapPath("wnew.txt"))



Make sure that you have permissions to delete a file.



15.11 How to find the date and time the specified file or directory was last written to? Use namespace System.IO VB.NET



dim path as string = Server.MapPath("page1.aspx") Response.Write ( File.GetLastWriteTime(path))



C#



string path =Server.MapPath("page1.aspx"); Response.Write ( File.GetLastWriteTime(path));



15.12 How to get the File information using ASP.NET? Use the namespace System.IO VB.NET



Dim fPath As String = Server.MapPath("orders.xml") Dim fInfo As New FileInfo(fPath) Dim strFileInfo As String If fInfo.Exists Then strFileInfo = "Name: " + fInfo.Name + "" strFileInfo += "Location: " + fInfo.FullName + "" strFileInfo += "Created on: " + fInfo.CreationTime + "" strFileInfo += "Extension: " + fInfo.Extension Else strFileInfo = "The file " + fPath + " was not found." End If



Response.Write(strFileInfo)



C#



string fPath = Server.MapPath("orders.xml"); FileInfo fInfo = new FileInfo(fPath); string strFileInfo ; if(fInfo.Exists) { strFileInfo = "Name: " + fInfo.Name + ""; strFileInfo += "Location: " + fInfo.FullName + ""; strFileInfo += "Created on: " + fInfo.CreationTime + ""; strFileInfo += "Extension: " + fInfo.Extension; } else { strFileInfo = "The file " + fPath + " was not found."; } Response.Write (strFileInfo);



15.13 How to create a .csv file that grabs the data from the database?







VB.NET



Dim Dim Dim Dim Dim Dim



cn As SqlConnection cmd As SqlCommand filename As String dr As SqlDataReader i As Integer sb As System.Text.StringBuilder



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click cn = New SqlConnection("server=localhost;uid=sa;pwd=;database=northwind") filename = "products.csv" cmd = New SqlCommand("select * from products ", cn) cmd.Connection.Open() dr = cmd.ExecuteReader(CommandBehavior.CloseConnection) sb = New System.Text.StringBuilder 'For field Names For i = 0 To dr.FieldCount - 1 If i ")) sr.Close()



C#



StreamReader sr = File.OpenText(Server.MapPath("1.txt")); string strContents = sr.ReadToEnd(); //To display normal raw contents Response.Write(strContents); //To handle Carriage returns Response.Write(strContents.Replace("\n" , "")); sr.Close();



15.15 How to read specific characters from a text file? Use namespace System.IO VB.NET



Dim fs As New System.IO.FileStream(Server.MapPath("1.txt"), IO.FileMode.Open) Dim buffer(5) As Byte ' 5=> number of characters to be read fs.Read(buffer, 0, 5) fs.Close() Dim filechars() As Char = System.Text.Encoding.ASCII.GetChars(buffer) Dim filestring As String = New String(filechars) Response.Write(filestring)



C#



FileStream fs =new FileStream(Server.MapPath("1.txt"), FileMode.Open); Byte[] buffer= new byte [5] ; //5 => Number of characters to be read fs.Read(buffer, 0, 5); fs.Close(); Char[] filechars = System.Text.Encoding.ASCII.GetChars(buffer); string filestring = new String(filechars); Response.Write(filestring);



15.16 How to check files exist in a particular directory? Try to use "System.IO.File.Exists("path")". path is the physical file path and it will return you a boolean value.



15.17 What is a MemoryStream and how to use MemoryStream in ASP.NET? The MemoryStream class creates streams that use memory as storage instead of a disk or a network connection.



MemoryStream encapsulates data stored as an unsigned byte array that is initialized upon creation of a MemoryStream object, or the array can be created as empty. The encapsulated data is directly accessible in memory. Memory streams can reduce the need for temporary buffers and files in an application. For more details refer MemoryStream Class How can I display an image from a Sql Server database?



15.18 How to detect if the string indicating a file-system resource is a file or directory? VB.NET



Dim path As String = Server.MapPath("webform1.aspx") ' Put user code to initialize the page here If(File.GetAttributes(path) And FileAttributes.Directory) = FileAttributes.Directory Then Response.Write("Its a directory") Else Response.Write("Its a file") End If



C#



string path =Server.MapPath ("webform1.aspx"); if ((File.GetAttributes(path) & FileAttributes.Directory) ==FileAttributes.Directory) { Response.Write ("Its a directory"); } else { Response.Write ("Its a file"); }



© 2001-2008 Copyright Syncfusion Inc. All rights reserved. | Privacy Policy | Contact | Sitemap




About
I am Computer Professional, did my MCA and seeking a Job in Software industry. I love computers
Other docs by aswath ramacha...
Computer Notes
Views: 39  |  Downloads: 1
JAVA
Views: 7  |  Downloads: 0
Interview questions
Views: 5  |  Downloads: 0
Interview questions
Views: 12  |  Downloads: 1
Wireless Networking
Views: 94  |  Downloads: 2
Computer Notes
Views: 15  |  Downloads: 0
Computer Notes
Views: 111  |  Downloads: 0
Wireless Networking
Views: 2  |  Downloads: 0
Interview questions
Views: 5  |  Downloads: 0
Interview questions
Views: 101  |  Downloads: 3
Related docs
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!