2012-04-22 21 views
0

我有我想轉換爲ASP.NET C#數組轉換到ASP.NET C#和文件目錄枚舉

這裏PHP腳本是PHP:

<?php 

/* Configuration Start */ 

$thumb_directory = 'img/thumbs'; 
$orig_directory = 'img/original'; 

$stage_width=600; // How big is the area the images are scattered on 
$stage_height=400; 

/* Configuration end */ 

$allowed_types=array('jpg','jpeg','gif','png'); 
$file_parts=array(); 
$ext=''; 
$title=''; 
$i=0; 

/* Opening the thumbnail directory and looping through all the thumbs: */ 

$dir_handle = @opendir($thumb_directory) or die("There is an error with your image directory!"); 

$i=1; 
while ($file = readdir($dir_handle)) 
{ 
    /* Skipping the system files: */ 
    if($file=='.' || $file == '..') continue; 

    $file_parts = explode('.',$file); 
    $ext = strtolower(array_pop($file_parts)); 

    /* Using the file name (withouth the extension) as a image title: */ 
    $title = implode('.',$file_parts); 
    $title = htmlspecialchars($title); 

    /* If the file extension is allowed: */ 
    if(in_array($ext,$allowed_types)) 
    { 
     /* Generating random values for the position and rotation: */ 
     $left=rand(0,$stage_width); 
     $top=rand(0,400); 
     $rot = rand(-40,40); 

     if($top>$stage_height-130 && $left > $stage_width-230) 
     { 
      /* Prevent the images from hiding the drop box */ 
      $top-=120+130; 
      $left-=230; 
     } 

     /* Outputting each image: */ 

     echo ' 
     <div id="pic-'.($i++).'" class="pic" style="top:'.$top.'px;left:'.$left.'px;background:url('.$thumb_directory.'/'.$file.') no-repeat 50% 50%; -moz-transform:rotate('.$rot.'deg); -webkit-transform:rotate('.$rot.'deg);"> 
     <a class="fancybox" rel="fncbx" href="'.$orig_directory.'/'.$file.'" target="_blank">'.$title.'</a> 
     </div>'; 
    } 
} 

/* Closing the directory */ 
closedir($dir_handle); 

?> 

我努力讓數組部分工作。 PHP是否設置了一個名爲file_parts的數組?這是否意味着我列舉了ASP.NET中的目錄?

這是我到目前爲止已經試過,在進行的工作:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class web_content_notes_Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     /* Configuration Start */ 
     string thumb_directory; 
     string orig_directory; 

     thumb_directory = ("img/thumbs"); 
     orig_directory = ("img/original"); 

     // How big is the area the images are scattered on 
     int stage_width; 
     int stage_height; 
     stage_width = 600; 
     stage_height = 400; 
     /* Configuration end */ 


     // array of allowed file type extensions 
     string[] allowed_types = { "bmp", "gif", "png", "jpg", "jpeg", "doc", "xls" }; 

     //declare the array 
     string[] file_parts; 
     string ext = ""; 
     string title = ""; 

     int i = 0; 

     /* Opening the thumbnail directory and looping through all the thumbs: */ 
     string dir_handle; 
     dir_handle = thumb_directory; 

     string file; 


     while (file = dir_handle); 
     { 
      Console.WriteLine(file_parts[i]); 
     } 


    //..... 


    } 
} 

我如何遍歷數組?有點困惑,因爲直到目錄被讀取,數組才被填充。

回答

1

您可以使用System.IO.DirectoryGetFiles方法(MSDN)來實現此目的;

for (string file in System.IO.Directory.GetFiles(thumb_directory)) { 
    Console.WriteLine(file); 
} 

這將是值得擁有看看System.IO.Path類(MSDN),因爲它爲您提供了一些不錯的方法,如GetFileNameWithoutExtension()GetExtension()

編輯:完全轉換;

protected void Page_Load(object sender, EventArgs e) { 
     /* Configuration Start */ 
     string thumb_directory = "img/thumbs"; 
     string orig_directory = "img/original"; 
     int stage_width = 600; 
     int stage_height = 480; 
     Random random = new Random(); 

     // array of allowed file type extensions 
     string[] allowed_types = { "bmp", "gif", "png", "jpg", "jpeg", "doc", "xls" }; 

     /* Opening the thumbnail directory and looping through all the thumbs: */ 
     foreach (string file in Directory.GetFiles(thumb_directory)) { 
      string title = Path.GetFileNameWithoutExtension(file); 
      if (allowed_types.Contains(Path.GetExtension(file)) == true) { 
       int left = random.Next(0, stage_width); 
       int top = random.Next(0, 400); 
       int rotation = random.Next(-40, -40); 

       if ((top > stage_height - 130) && (left > stage_width - 230)) { 
        top -= 120 + 130; 
        left -= 230; 
       } 
      } 
     } 
    } 
+0

謝謝,我的轉換看起來好嗎? – 2012-04-22 10:04:43

+0

是的,不是太糟糕。完成將它轉換爲你,除了輸出細節到瀏覽器。這將取決於您是否使用WebForms或MVC。 – MrMDavidson 2012-04-22 10:11:43

+0

你做了什麼?少用幾行代碼?哇! – 2012-04-22 10:12:23