ho fatto questo esempio
il primo pulsante crea 20 files nominati 1.txt, 2.txt, ..., 20.txt
il secondo pulsante visualizza la lista di quei files ordinati per nome, cioè da 1.txt, 2.txt,..., 20.txt
Siccome è un argomento che non conosco bene, e c# ancora meno, prego altri amici più esperti ad intervenire
codice:
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
/*--------------------------------------------------------------
scrivo 20 files in c:\tmp\tmp, da 1.txt a 20.txt
---------------------------------------------------------------*/
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 1; i <= 20; i++)
{
string s = i.ToString() + ".txt";
string f = Path.Combine("c:\\tmp\\tmp", s);
//libreria.ModuloWeb.PrintLn(f);
StreamWriter sw = File.CreateText(f);
sw.Close();
}
}
/*------------------------------------------------------------
* lista i files ordinandoli per nome (ordine numerico)
*
* ----------------------------------------------------------*/
protected void Button2_Click(object sender, EventArgs e)
{
string path = "c:\\tmp\\tmp";
string[] fs = Directory.GetFiles(path);
Array.Sort(fs, new CompareByName());
StringBuilder sb = new StringBuilder();
sb.Append("<Table>");
foreach (string f in fs)
{
//libreria.ModuloWeb.PrintLn(f);
sb.Append("<tr><td>" + f + "</td></tr>");
}
sb.Append("</table>");
this.form1.Controls.Add(new LiteralControl(sb.ToString()));
}
/*----------------------------------------------------------------
* classe ausiliaria per ordinare per nome di file (numerico, 10 > 2)
*
* --------------------------------------------------------------*/
public class CompareByName: IComparer<string>
{
public int Compare(string o1, string o2)
{
if (o1 == null && o2 == null) return 0;
if (o1 == null) return 1;
if (o2 == null) return -1;
int s1 = int.Parse(Path.GetFileNameWithoutExtension(o1));
int s2 = int.Parse(Path.GetFileNameWithoutExtension(o2));
if (s1 > s2)
return 1;
else if (s1 < s2)
return -1;
else return 0;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Pagina senza titolo</title>
<link href="../../stili/Styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="crea i files" /><asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Leggi files" /></div>
</form>
</body>
</html>