How to check if an item already exists in a listbox
VB.NET Dim lstitem As ListItem = ListBox1.Items.FindByValue(‘<valuecheckedfor>’) If Not lstitem Is Nothing Then Response.Write(‘Item Exists’) Else Response.Write(‘Item Does not exist’) End If C# ListItem lstitem = ListBox1.Items.FindByValue(‘<valuecheckedfor>’); if ( lstitem !=null) { Response.Write (‘Does exist’); } else { Response.Write (‘Does not exist’); } You can also use FindByText instead of FindByValue.
How to make the listbox scroll down to show it’s last entry when the page loads?
<p> <asp:ListBox id=’ListBox1′ runat=’server’></asp:ListBox> </p> <p> <asp:Button id=’buttonAdd’ runat=’server’ Text=’Add’></asp:Button> </p> In button Click Event VB.NET ListBox1.Items.Add(DateTime.Now.ToString(‘MMM dd, yyyy’) + ‘ ‘ + DateTime.Now.ToString(‘t’)) Dim scrollScript As String scrollScript &= ‘<script language=’javascript’>’ scrollScript &= ‘document.forms[0].ListBox1.selectedIndex ‘ & _ ‘ = document.forms[0].ListBox1.options.length-1;’ scrollScript &= ‘<‘ & ‘/’ & ‘script>’ Page.RegisterStartupScript(”, scrollScript) C# ListBox1.Items.Add(DateTime.Now.ToString(‘MMM dd, yyyy’) + ‘ ‘ + DateTime.Now.ToString(‘t’)); string scrollScript=” ; scrollScript += ‘<script language=’javascript’>’; scrollScript += ‘document.forms[0].ListBox1.selectedIndex = document.forms[0].ListBox1.options.length-1;’; scrollScript += ‘<‘ + ‘/’ + ‘script>’; Page.RegisterStartupScript(”, scrollScript);
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 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.