[英]convert array from php to asp.net c# and file directory enumeration
我有一个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]);
}
//.....
}
}
如何遍历数组? 有点困惑,因为直到读取目录才填充数组。
您可以使用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;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.