0

我有一个 PHP 脚本,我正在尝试将其转换为 ASP.NET C#

这是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]);
        }


     //.....


    }
}

如何循环遍历数组?有点困惑,因为在读取目录之前不会填充数组。

4

1 回答 1

1

您可以使用System.IO.Directory'GetFiles方法(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;
                }
            }
        }
    }
于 2012-04-22T10:03:21.840 回答