0

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


     //.....


    }
}

配列をループするにはどうすればよいですか? ディレクトリが読み込まれるまで配列が満たされないため、少し混乱しています。

4

1 に答える 1

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;
                }
            }
        }
    }
于 2012-04-22T10:03:21.840 に答える