将数组从php转换为asp.net c#和文件目录枚举
我有一个PHP脚本,我正试图将其转换为ASP.NET C# 以下是PHP:将数组从php转换为asp.net c#和文件目录枚举,c#,php,asp.net,C#,Php,Asp.net,我有一个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
<?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);
?>
如何在数组中循环?有点困惑,因为直到读取目录后才填充数组。您可以使用
System.IO.directory
的GetFiles
方法()来实现这一点
for (string file in System.IO.Directory.GetFiles(thumb_directory)) {
Console.WriteLine(file);
}
值得一看System.IO.Path
class(),因为它提供了一些很好的方法,比如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;
}
}
}
}
是的,还不错。已为您完成转换,但将详细信息输出到浏览器除外。这将取决于你是使用WebForms还是MVC。好吧,它实际上没有对你计算的图像路径/位置做任何有用的事情,但是是的。我建议您查看并移动实际的配置内容(例如路径)。并移动到使用符号常量`const int stage_width=600;“(计算中的值也是如此。)但希望你能理解。是的,我想是的..哇,你真的是个好人!我现在可以研究你提到的东西了。上帝保佑你!:)必须用这个来代替:if(allowed_types.Equals(Path.GetExtension(file))==true)as contains不起作用。
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;
}
}
}
}