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.
You can use regular expressions to parse delimited text, for example, very easily. Here is an article from ftp online: Parse Text Files With Regular Expressions that discusses this in more detail.
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.
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#
stringpath =Server.MapPath ('webform1.aspx');
if ((File.GetAttributes(path) & FileAttributes.Directory) ==FileAttributes.Directory)
{
Response.Write ('Its a directory');
}
else
{
Response.Write ('Its a file');
}
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 asException
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 );
}
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.
ProtectedFunctionGetCode(filename As String)AsStringDimsrAsNewStreamReader(filename)DimsbAsNewStringBuilder()sb.Append('<code><pre>')sb.Append(sr.ReadToEnd())sb.Append('</pre></code>')sr.Close()Returnsb.ToString()sb = Nothingsr = NothingEndFunction ’GetCodePrivateSubButton1_Click(sender As Object, e As System.EventArgs)Response.Write(GetCode((Server.MapPath('WebForm1.aspx') + '.vb')))EndSub ’Button1_Click
Dim sr As StreamReader
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>'))
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' , '<br>'));
sr.Close();
Dimfile As String = Server.MapPath('temp.html')Dimsr As StreamReaderDimfi As New FileInfo(file)Diminput As String = '<pre>'IfFile.Exists(file) Thensr = File.OpenText(file)input+= Server.HtmlEncode(sr.ReadToEnd())sr.Close()EndIfinput+= '</pre>'Me.Label1.Text = input
Dim cn As SqlConnection
Dim cmd As SqlCommand
Dim filename As String
Dim dr As SqlDataReader
Dim i As Integer
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)
cmd.Connection.Open()
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
sb = New System.Text.StringBuilder
’For field Names
For i = 0 To dr.FieldCount - 1If i < (dr.FieldCount - 1) Then
sb.Append(Chr(34) & dr.GetName(i) & _
Chr(34) & ',')
Else
sb.Append(Chr(34) & dr.GetName(i) & _
Chr(34) & vbCrLf)
End If
Next
’For field Values
While dr.Read()
For i = 0 To dr.FieldCount - 1If i < (dr.FieldCount - 1) Then
sb.Append(Chr(34) & _
dr.GetValue(i).ToString & Chr(34) & ',')
Else
sb.Append(Chr(34) & _
dr.GetValue(i).ToString & Chr(34) & vbCrLf)
End If
Next
End While
dr.Close()
cn.Close()
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)
Response.End()
End Sub
C#
SqlConnection cn ;
SqlCommand cmd ;
string filename ;
SqlDataReader dr ;
System.Text.StringBuilder sb ;
privatevoidButton1_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);
cmd.Connection.Open();
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
sb = new System.Text.StringBuilder();
//For field Namesfor(int i = 0 ;i<= dr.FieldCount - 1;i++)
{
if( i < (dr.FieldCount - 1) )
{
sb.Append('\t' + dr.GetName(i) + '\t' + ',');
}
else
{
sb.Append('\t' + dr.GetName(i) + '\t' + '\n');
}
}
//For field Valueswhile( dr.Read())
{
for(int i = 0 ;i<= dr.FieldCount - 1;i++)
{
if( i < (dr.FieldCount - 1) )
{
sb.Append('\t' + dr.GetValue(i) + '\t' + ',');
}
else
{
sb.Append('\t' + dr.GetValue(i) + '\t' + '\n');
}
}
}
dr.Close();
cn.Close();
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());
Response.End();
}
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.
Dim fs AsNew 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= newbyte [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);