Académique Documents
Professionnel Documents
Culture Documents
15.1 What is the best way to rename a file on the webserver in code?
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.
VB.NET
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 );
}
Use namespace
• System.IO
• System.Text
VB.NET
C#
VB.NET
C#
15.5 How can I to get the path to the system area that holds temporary
files?
VB.NET
C#
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
Use Request.Path
VB.NET
Response.Write (Path.GetFileName(Request.PhysicalPath))
C#
Response.Write (Path.GetFileName(Request.PhysicalPath));
• VB.NET code
• C# Code
VB.NET
System.IO.File.Delete(Server.MapPath("wnew.txt"))
C#
System.IO.File.Delete(Server.MapPath("wnew.txt"))
15.11 How to find the date and time the specified file or directory was
last written to?
VB.NET
C#
VB.NET
C#
15.13 How to create a .csv file that grabs the data from the database?
VB.NET
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
C#
SqlConnection cn ;
SqlCommand cmd ;
string filename ;
SqlDataReader dr ;
System.Text.StringBuilder sb ;
VB.NET
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)
sr.Close()
C#
StreamReader sr = File.OpenText(Server.MapPath("1.txt"));
string strContents = sr.ReadToEnd();
//To display normal raw contents
Response.Write(strContents);
sr.Close();
VB.NET
C#
Try to use "System.IO.File.Exists("path")". path is the physical file path and it will return you a boolean value.
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.
• MemoryStream Class
VB.NET
C#