|
|
18.1 Are there ways to parse text files with Regular Expressions?
|
 |
18.2 How to remove a Key From an INI File?
|
 |
Here is an article from ftp online: Remove a Key From an INI File that describes how this can be done using the native WritePrivateProfileString and GetPrivateProfileString methods. |
18.3 How to create a folder in ASP.NET?
|
 |
Use System.IO namespace
VB.NET
|
' Determine whether the directory exists.
|
If Directory.Exists(path) Then
|
Response.Write("That path exists already.")
|
' Try to create the directory.
|
Dim di As DirectoryInfo = Directory.CreateDirectory(path)
|
Response.Write(("Directory create successfully at " + Directory.GetCreationTime(path)))
|
Response.Write (ex.Message )
|
string path = @"c:\MyDir";
|
// Determine whether the directory exists.
|
if (Directory.Exists(path))
|
Response.Write ("That path exists already.");
|
// Try to create the directory.
|
DirectoryInfo di = Directory.CreateDirectory(path);
|
Response.Write("Directory create successfully at " + Directory.GetCreationTime(path));
|
Response.Write (ex.Message );
|
18.4 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")
|
File.Move(@"C:\Dir1\SomeFile.txt", @"C:\Dir1\RenamedFileName.txt")
|
Refer
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.
|
18.5 How to delete a file from the server?
|
 |
System.IO.File.Delete(Server.MapPath("wnew.txt"))
|
System.IO.File.Delete(Server.MapPath("wnew.txt"))
|
Make sure that you have permissions to delete a file. |
18.6 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))
|
string path =Server.MapPath("page1.aspx");
|
Response.Write ( File.GetLastWriteTime(path));
|
18.7 How to get the File information using ASP.NET?
|
 |
Use the namepsace System.IO
VB.NET
|
Dim fPath As String = Server.MapPath("orders.xml")
|
Dim fInfo As New FileInfo(fPath)
|
Dim strFileInfo As String
|
strFileInfo = "Name: " + fInfo.Name + "<br />"
|
strFileInfo += "Location: " + fInfo.FullName + "<br />"
|
strFileInfo += "Created on: " + fInfo.CreationTime + "<br />"
|
strFileInfo += "Extension: " + fInfo.Extension
|
strFileInfo = "The file <b>" + fPath + "</b> was not found."
|
Response.Write(strFileInfo)
|
string fPath = Server.MapPath("orders.xml");
|
FileInfo fInfo = new FileInfo(fPath);
|
strFileInfo = "Name: " + fInfo.Name + "<br />";
|
strFileInfo += "Location: " + fInfo.FullName + "<br />";
|
strFileInfo += "Created on: " + fInfo.CreationTime + "<br />";
|
strFileInfo += "Extension: " + fInfo.Extension;
|
strFileInfo = "The file <b>" + fPath + "</b> was not found.";
|
Response.Write (strFileInfo);
|
18.8 How to create a .csv file that grabs the data from the database?
|
 |
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
|
Dim 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)
|
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
|
sb = New System.Text.StringBuilder
|
For i = 0 To dr.FieldCount - 1
|
If i < (dr.FieldCount - 1) Then
|
sb.Append(Chr(34) & dr.GetName(i) & _
|
sb.Append(Chr(34) & dr.GetName(i) & _
|
For i = 0 To dr.FieldCount - 1
|
If i < (dr.FieldCount - 1) Then
|
dr.GetValue(i).ToString & Chr(34) & ",")
|
dr.GetValue(i).ToString & Chr(34) & vbCrLf)
|
Response.ContentType = "Application/x-msexcel"
|
("content-disposition", "attachment; filename=""" & _
|
'Write the file directly to the HTTP output stream.
|
Response.Write(sb.ToString)
|
System.Text.StringBuilder sb ;
|
private void Button1_Click(object sender, System.EventArgs e)
|
cn = new SqlConnection("server=localhost;uid=sa;pwd=;database=northwind");
|
filename = "products.csv";
|
cmd = new SqlCommand("select * from products ", cn);
|
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
|
sb = new System.Text.StringBuilder();
|
for(int i = 0 ;i<= dr.FieldCount - 1;i++)
|
if( i < (dr.FieldCount - 1) )
|
sb.Append("\t" + dr.GetName(i) + "\t" + ",");
|
sb.Append("\t" + dr.GetName(i) + "\t" + "\n");
|
for(int i = 0 ;i<= dr.FieldCount - 1;i++)
|
if( i < (dr.FieldCount - 1) )
|
sb.Append("\t" + dr.GetValue(i) + "\t" + ",");
|
sb.Append("\t" + dr.GetValue(i) + "\t" + "\n");
|
Response.ContentType = "Application/x-msexcel";
|
Response.AddHeader ("content-disposition", "attachment; filename=" + filename ) ;
|
//Write the file directly to the HTTP output stream.
|
Response.Write(sb.ToString());
|
18.9 How to read text file in ASP.NET?
|
 |
Use namespace System.IO
VB.NET
|
sr = File.OpenText(Server.MapPath("1.txt"))
|
Dim strContents As String = sr.ReadToEnd()
|
'To display normal raw contents
|
Response.Write(strContents)
|
'To handle Carriage returns
|
Response.Write(strContents.Replace(vbCrLf, "<br>"))
|
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" , "<br>"));
|
18.10 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
|
Dim filechars() As Char = System.Text.Encoding.ASCII.GetChars(buffer)
|
Dim filestring As String = New String(filechars)
|
Response.Write(filestring)
|
FileStream fs =new FileStream(Server.MapPath("1.txt"), FileMode.Open);
|
Byte[] buffer= new byte [5] ; //5 => Number of characters to be read
|
Char[] filechars = System.Text.Encoding.ASCII.GetChars(buffer);
|
string filestring = new String(filechars);
|
Response.Write(filestring);
|
18.11 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. |
18.12 How to read a html file in ASP.NET?
|
 |
Use namespace System.IO
VB.NET
|
Dim file As String = Server.MapPath("temp.html")
|
Dim fi As New FileInfo(file)
|
Dim input As String = "<pre>"
|
If File.Exists(file) Then
|
input += Server.HtmlEncode(sr.ReadToEnd())
|
string file = Server.MapPath ("temp.html");
|
FileInfo fi = new FileInfo(file);
|
sr = File.OpenText(file);
|
input += Server.HtmlEncode(sr.ReadToEnd());
|
this.Label1.Text = input;
|
18.13 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 + " "))
|
string filePath =Path.GetTempPath ();
|
string fileName = Path.GetTempFileName();
|
Response.Write (filePath + " " );
|
Response.Write (fileName );
|
18.14 How to show the ASP.NET code to the users?
|
 |
Protected Function GetCode(filename As String) As String
|
Dim sr As New StreamReader(filename)
|
Dim sb As New StringBuilder()
|
sb.Append(sr.ReadToEnd())
|
sb.Append("</pre></code>")
|
Private Sub Button1_Click(sender As Object, e As System.EventArgs)
|
Response.Write(GetCode((Server.MapPath("WebForm1.aspx") + ".vb")))
|
protected string GetCode(string filename)
|
StreamReader sr =new StreamReader(filename );
|
StringBuilder sb =new StringBuilder();
|
sb.Append("<code><pre>");
|
sb.Append(sr.ReadToEnd());
|
sb.Append("</pre></code>");
|
private void Button1_Click(object sender, System.EventArgs e)
|
Response.Write (GetCode(Server.MapPath ("WebForm1.aspx") + ".cs"));
|
18.15 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. |
18.16 How to get the physical path of a file?
|
 |
18.17 How to get the current filename?
|
 |
Response.Write (Path.GetFileName(Request.PhysicalPath))
|
Response.Write (Path.GetFileName(Request.PhysicalPath));
|
18.18 How to Upload files in ASP.NET?
|
 |
Take a look at following articles
|
18.19 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
|
18.20 How to detect if the string indicating a file-system resource is a file or directory?
|
 |
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")
|
Response.Write("Its a file")
|
string path =Server.MapPath ("webform1.aspx");
|
if ((File.GetAttributes(path) & FileAttributes.Directory) ==FileAttributes.Directory)
|
Response.Write ("Its a directory");
|
Response.Write ("Its a file");
|
|
|
|
|