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’); }

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 If fInfo.Exists Then strFileInfo = ‘Name: ‘ + fInfo.Name + ‘<br />’ strFileInfo += ‘Location: ‘ + fInfo.FullName + ‘<br />’ strFileInfo += ‘Created on: ‘ + fInfo.CreationTime + ‘<br />’ strFileInfo += ‘Extension: ‘ + fInfo.Extension Else strFileInfo = ‘The file <b>’ + fPath + ‘</b> 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 + ‘<br />’; strFileInfo += ‘Location: ‘ + fInfo.FullName + ‘<br />’; strFileInfo += ‘Created on: ‘ + fInfo.CreationTime + ‘<br />’; strFileInfo += ‘Extension: ‘ + fInfo.Extension; } else { strFileInfo = ‘The file <b>’ + fPath + ‘</b> was not found.’; } Response.Write (strFileInfo);

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 ); }

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.