Récupérer l'extension d'un fichier fileUpload.saveAs()

cs_polo86 Messages postés 19 Date d'inscription samedi 14 février 2009 Statut Membre Dernière intervention 5 mai 2009 - 21 avril 2009 à 18:47
titeoe Messages postés 433 Date d'inscription samedi 26 avril 2003 Statut Membre Dernière intervention 4 décembre 2009 - 23 avril 2009 à 08:43
Salut,


J'ajoute mon fichier dans un dossier spécifié dans mon web.config grace au composant fileUpload

Code :
if(fUpDocument.HasFile)
{
string path = TfeConfiguration.Path;
path += "\" + ddlProjet.SelectedItem.ToString();
try
{
path += "\" + Server.HtmlEncode(fUpDocument.FileName);
 
fUpDocument.SaveAs(path);
 
}
}

J'affiche ensuite tous mes dossiers et fichiers dans un treeView

Code :
if(Directory.Exists(path))
{
char[] wordSeparators = newchar[]{',', ';', '!', '?', '+', '\\'};
char[] wordSeparatorsBis = newchar[]{',', ';', '!', '?', '+', '.'};
string[] files; // pour avoir les noms des fichiers et sous-répertoires
files = Directory.GetFileSystemEntries(path);
int filecount = files.GetUpperBound(0) + 1;
for(int i = 0; i < filecount; i++)
{
string[] words = files[i].Split(wordSeparators,
StringSplitOptions.RemoveEmptyEntries);
TreeNode n = new TreeNode(files[i]);
 
string[] tab = {"gif", "jpeg", "pdf", "doc"};
n.Text = words[words.Count() - 1];

DirectoryInfo f = new DirectoryInfo(files[i]);
if(f.Parent.Parent.Parent != null)
{
n.ImageUrl = "~/icone/rep.jpg";
tVDocument.Nodes.Add(n);
}
else
{
n.ImageUrl = "~/icone/repRoot.jpg";
tVDocument.Nodes.Add(n);
 
path = files[i];
string[] filesEnfant = Directory.GetFileSystemEntries(path);//récupère les fichiers des sous dossiers
if(filesEnfant.Count() > 0)
{
for(int j = 0; j < filesEnfant.Count(); j++)
{
 
string type = GetMIMEType(path);
//Type te = filesEnfant[j].GetType();
n = null;
//filesEnfant[j]
n = new TreeNode(filesEnfant[j]);
n.ImageUrl = "~/icone/" + type + ".gif";
n.SelectAction = TreeNodeSelectAction.Expand;
n.Text = words[words.Count() - 1];
tVDocument.Nodes[i].ChildNodes.Add(n);
//tVDocument.Nodes.Add(n);
}
}

}
}
}

}
 
}
publicstring GetMIMEType(string path)
{
RegistryPermission regPerm = new RegistryPermission(RegistryPermissionAccess.Read, "\\HKEY_CLASSES_ROOT");
RegistryKey classesRoot = Registry.ClassesRoot;
FileInfo fi = new FileInfo(path);
String dotExt = fi.Extension.ToLower();//aucune extension n'est trouvée
RegistryKey typeKey = classesRoot.OpenSubKey(@"MIME\Database\Content Type");
String Keyname = "";
foreach(string keyname in typeKey.GetSubKeyNames())
{
RegistryKey curKey = classesRoot.OpenSubKey(@"MIME\Database\Content Type" + keyname);
string test = (String)curKey.GetValue("Extension");
if((String)curKey.GetValue("Extension") == dotExt)
{
Keyname = keyname;
}
}
return Keyname;

}

Voilà mon problème est que je ne récupère aucune extension pour le fichier et je ne vois pas du tout pourquoi.


Merci de votre aide

1 réponse

titeoe Messages postés 433 Date d'inscription samedi 26 avril 2003 Statut Membre Dernière intervention 4 décembre 2009 1
23 avril 2009 à 08:43
Bonjour,

Si tu cherches l'extension d'un fichier, pourquoi ne pas utiliser simplement la propriété "Extension" de la classe FileInfo ?

doc MSDN : http://msdn.microsoft.com/en-us/library/system.io.filesysteminfo.extension.aspx
0
Rejoignez-nous