随机函数

本文介绍了一段使用C#编写的程序,该程序能够在指定目录下随机搜索一定比例的文件和子目录,并将结果展示在网页上。程序首先读取目录信息,然后使用随机数生成器选择文件进行显示。
下面这段代码是自己写的一段程序段,主要功能是在一个确定的路径下面随机搜索n个项目.

None.gifprotected void Button1_Click(object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Label3.Visible 
= true;
InBlock.gif            Label3.Text 
= "";
InBlock.gif            TableRow tr;
InBlock.gif            TableCell tc;
InBlock.gif            Random autoRand 
= new Random();
InBlock.gif            ArrayList arr 
= new ArrayList();
InBlock.gif            
double percentage = Convert.ToInt32(TextBox3.Text);
InBlock.gif            percentage 
= percentage / 100;
InBlock.gif
InBlock.gif            
string name = TextBox1.Text;
InBlock.gif            
string str = "D:\\XXX\\" + name + "\\";
InBlock.gif            
int num = 0;
InBlock.gif
InBlock.gif            DirectoryInfo dir 
= new DirectoryInfo(str);
InBlock.gif
InBlock.gif            
foreach (FileSystemInfo fsi in dir.GetFileSystemInfos())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                filename 
= "";
InBlock.gif                
if (fsi is FileInfo)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    num
++;
InBlock.gif                    filename 
= fsi.FullName;
InBlock.gif                    arr.Add(filename);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (fsi is DirectoryInfo)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    num
++;
InBlock.gif                    filename 
= fsi.FullName;
InBlock.gif                    arr.Add(filename);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
for (double i = 1; i <=percentage * num; i += 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int sum =Convert.ToInt32(percentage * num);
InBlock.gif                
foreach (FileSystemInfo fsi in dir.GetFileSystemInfos())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    filename 
= arr[autoRand.Next(1, num)].ToString();
InBlock.gif                    tr 
= new TableRow();
InBlock.gif                    tc 
= new TableCell();
InBlock.gif                    tc.Controls.Add(
new LiteralControl(filename.ToString()));
InBlock.gif                    tr.Cells.Add(tc);
InBlock.gif
InBlock.gif                    Table1.Rows.Add(tr);
InBlock.gif                    
break;
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Label3.Text 
= "不存在该项目,请确定后重新搜索!";
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值