How to loop through all files in a folder using C#

Bellow is the code for loop throught all files in a folder.

// How much deep to scan. (of course you can also pass it to the method)
const int HowDeepToScan=4;

public static void ProcessDir(string sourceDir, int recursionLvl) 
{
  if (recursionLvl<=HowDeepToScan)
  {
    // Process the list of files found in the directory.
    string [] fileEntries = Directory.GetFiles(sourceDir);
    foreach(string fileName in fileEntries)
    {
       // do something with fileName
       Console.WriteLine(fileName);
    }

    // Recurse into subdirectories of this directory.
    string [] subdirEntries = Directory.GetDirectories(sourceDir);
    foreach(string subdir in subdirEntries)
       // Do not iterate through reparse points
       if ((File.GetAttributes(subdir) &
            FileAttributes.ReparsePoint) !=
                FileAttributes.ReparsePoint)

            ProcessDir(subdir,recursionLvl+1);
  }
}

How To Make Directory In ASP.NET?

To create directory on web server with ASP.NET server side code, you can use this code:

[ C# ]

using System;
using System.Data;
using System.Configuration;
// We need this namespace to create directory
using System.IO;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string NewDir = Server.MapPath("New Folder");
        // Call function for creating a directory
        MakeDirectoryIfExists(NewDir);
    }
 
    private void MakeDirectoryIfExists(string NewDirectory)
    {
        try
        {
            // Check if directory exists
            if (!Directory.Exists(NewDirectory))
            {
                // Create the directory.
                Directory.CreateDirectory(NewDirectory);
            }
        }
        catch (IOException _ex)
        {
            Response.Write(_ex.Message);
        }
    }
}

[ VB.NET ]


' We need this namespace to make a directory
Imports System.IO
 
Partial Class _Default
    Inherits System.Web.UI.Page
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim NewDir As String = Server.MapPath("New Folder")
        ' Call function for creating a directory
        MakeDirectoryIfExists(NewDir)
    End Sub
 
    Private Sub MakeDirectoryIfExists(ByVal NewDirectory As String)
        Try
            ' Check if directory exists
            If Not Directory.Exists(NewDirectory) Then
                ' Create the directory.
                Directory.CreateDirectory(NewDirectory)
            End If
        Catch _ex As IOException
            Response.Write(_ex.Message)
        End Try
    End Sub

Show all images in a directory asp.net

How can we show all image from a particular directory in asp.net.

  <asp:DataList ID="dlQLImages" runat="server">  
      <ItemTemplate>  
        <img src='<%# string.Format("~/Images/{0}", DataBinder.Eval(Container.DataItem,"Name")) %>'  
            height="250" width="400" runat="server" />  
      </ItemTemplate>  
  </asp:DataList>  
   If Not IsPostBack Then  
        Dim objDI As New System.IO.DirectoryInfo(Server.MapPath("~/Images"))  
        Me.dlQLImages.DataSource = objDI.GetFiles("*.png")  
       Me.dlQLImages.DataBind()  
    End If  

or you can use

 
  DirectoryInfo diImages = new DirectoryInfo(Server.MapPath("~/images/Slider/"));

            
            ArrayList alImages = new ArrayList();
            // GetFiles method doesn't allow us to filter for multiple
            // file extensions, so we must find images in four steps
            // Feel free to add new or remove existing extension to
            // suit your needs
            alImages.AddRange(diImages.GetFiles("*.gif"));
            alImages.AddRange(diImages.GetFiles("*.jpg"));
            alImages.AddRange(diImages.GetFiles("*.bmp"));
            alImages.AddRange(diImages.GetFiles("*.png"));

            dtlSlider.DataSource =alImages;
            dtlSlider.DataBind();

Remove the default interface CakePHP links in default Cake apps?

Reformatting the default CakePHP layout (default.ctp)

Modify app/views/layouts/default.ctp

If no view exists in that location, check cake/libs/views/layout/default.ctp

Then copy the default.ctp like your layout.

Bellow is a sampel modification which we do for testing . This will remove the title and footer and default css of cake php template. You can modify it according to your need.


[smProductImageAdd src="http://learneveryday.net/codecanyon/adverticement/add_codecnayon_smart-social-share-php.png" alt= "Smart Social Share" href="http://codecanyon.net/item/php-smart-social-share/202558" title="Smart Social Share (Php Plugin)" description="Smart Social share is a php plugin . Which helps you to give user to share your content with social book mark site. There are 4 plugin in this package." ref="marifdu"] 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<?php echo $this->Html->charset(); ?>

	<?php
		echo $this->Html->meta('icon');

		echo $scripts_for_layout;
	?>
</head>
<body>
	<div id="container">

		<div id="content">

			<?php echo $this->Session->flash(); ?>

			<?php echo $content_for_layout; ?>

		</div>
	
	</div>
</body>
</html>

[smProductImageAdd src="http://learneveryday.net/codecanyon/adverticement/add_codecnayon_smart-social-share-php.png" alt= "Smart Social Share" href="http://codecanyon.net/item/php-smart-social-share/202558" title="Smart Social Share (Php Plugin)" description="Smart Social share is a php plugin . Which helps you to give user to share your content with social book mark site. There are 4 plugin in this package." ref="marifdu"]

Undefined variable: javascript in cakePHP with scriptaculous

Today i am trying to use ajax call in cakephp.

<?php  echo $ajax->submit('Submit', array('url'=> array('controller'=>'users', 'action'=>'add'), 'update' => 'testdiv')); ?>

I found this error:

Undefined variable: Javascript

After a couple of searching I found the following things can cause this problem:

1. You do not have the html, javascript and ajax helper defined in your own controller (eg. user_controller.php)

var $helpers = array('Html','Javascript','Ajax'); 

2. Your render action points to a view where no view file is existing for. ( so the view file HAS to be created!

$this->render('the_method','ajax'); 

Hope it helps somebody who is experiencing the same problem I had.