2011-07-27 6 views
0

こんにちは私はWebベースのFTPクライアントアプリケーションを開発しています。私はクライアントファイルシステムディレクトリを取得し、それらをツリービューに取り込みたいのですが、私のアプリケーションが実行されているシステム(サーバー)は、私は任意のユーザーがブラウザを介して私のアプリケーションにアクセスするとき、私はユーザーのファイルシステムのディレクトリをロードしたい。treeview asp.netのクライアントファイルシステムディレクトリを取得する方法C#

これは私が試したコードです:

private void fillTree() 
     { 
      DirectoryInfo directory; 
      string sCurPath = ""; 

      // clear out the old values 
      TreeView2.Nodes.Clear(); 

      // loop through the drive letters and find the available drives. 
      foreach (char c in driveLetters) 
      { 
       sCurPath = c + ":\\"; 
       try 
       { 
        // get the directory informaiton for this path. 
        directory = new DirectoryInfo(sCurPath); 

        // if the retrieved directory information points to a valid 
        // directory or drive in this case, add it to the root of the 
        // treeView. 
        if (directory.Exists == true) 
        { 
         TreeNode newNode = new TreeNode(directory.FullName); 
         TreeView2.Nodes.Add(newNode); // add the new node to the root level. 
         getSubDirs(newNode);   // scan for any sub folders on this drive. 
        } 
       } 
       catch (Exception doh) 
       { 
        lblStatus.Text = doh.Message; 
       } 
      } 
     } 
     private void getSubDirs(TreeNode parent) 
     { 
      DirectoryInfo directory; 
      try 
      { 
       // if we have not scanned this folder before 
       if (parent.ChildNodes.Count == 0) 
       { 
        directory = new DirectoryInfo(parent.ValuePath); 
        foreach (DirectoryInfo dir in directory.GetDirectories()) 
        { 
         TreeNode newNode = new TreeNode(dir.Name); 
         parent.ChildNodes.Add(newNode); 
        } 
       } 

       // now that we have the children of the parent, see if they 
       // have any child members that need to be scanned. Scanning 
       // the first level of sub folders insures that you properly 
       // see the '+' or '-' expanding controls on each node that represents 
       // a sub folder with it's own children. 
       foreach (TreeNode node in parent.ChildNodes) 
       { 
        // if we have not scanned this node before. 
        if (node.ChildNodes.Count == 0) 
        { 
         // get the folder information for the specified path. 
         directory = new DirectoryInfo(node.ValuePath); 

         // check this folder for any possible sub-folders 
         foreach (DirectoryInfo dir in directory.GetDirectories()) 
         { 
          // make a new TreeNode and add it to the treeView. 
          TreeNode newNode = new TreeNode(dir.Name); 
          node.ChildNodes.Add(newNode); 
         } 
        } 
       } 
      } 
      catch (Exception doh) 
      { 
       lblStatus.Text = doh.Message; 
       // Console.WriteLine(doh.Message); 
      } 
     } 
     private string fixPath(TreeNode node) 
     { 
      string sRet = ""; 
      try 
      { 
       sRet = node.ValuePath; 
       int index = sRet.IndexOf("\\\\"); 
       if (index > 1) 
       { 
        sRet = node.ValuePath.Remove(index, 1); 
       } 
      } 
      catch (Exception doh) 
      { 
       Console.WriteLine(doh.Message); 
      } 
      return sRet; 
     } 

いずれかが正しく、このタスクを実行する方法を手伝ってくれる。

答えて

1

他の人によると、サーバー側のコードはクライアントのファイルシステムを読み取ることができません。

あなたの最良の選択肢は、Javaアプレット(afaik署名付きアプレットはファイルシステムにアクセスすることが許可されています)に書き込んで署名し、アプレットをウェブページに埋め込むことです。 ActiveXもオプションですが、Internet Explorerに限ります。

2

表示されているコードはサーバーで実行されます。そこのクライアントフォルダにアクセスすることはできません。あなたは通常、ブラウザがファイルシステムへのアクセスを許可しないスクリプトまたはプログラムとアクセス

を実行しているクライアント側が必要となりますことを行うには

。この方法を調べるには、次の質問をご覧ください。Browser application & local file system access

1

クライアントファイルシステムにアクセスして、ASP .NETまたはJavaScriptを使用してファイルを作成することはできません。

+0

私は自分のアプリケーションでユーザーディレクトリをどのように表示しますか?私はそれがサーバー上にアップロードするために1つを選択するユーザーシステムのすべてのフォルダを表示したい –

+0

そのために、あなたはあらかじめ構築された無料の商用製品がたくさんあります。 http://www.codeproject.com/KB/aspnet/FlashUpload.aspxまたはhttp://asp.net-tutorials.com/controls/file-upload-control/ OR http:// mattbersethをご覧ください。 com/blog/2008/07/aspnet_file_upload_with_realti_1.htmlまたはhttp://krystalware.com/slickuploadまたはhttp://www.uploadify.com/demos/ –

0

JavaScriptを試すことができます。それはあなたがドライブ内のファイルのリストを取得することができます。

<script> 
var Fo =new ActiveXObject("Scripting.FileSystemObject"); 
var StrOut = new String(); 
var FileName = new String(); 
var Extention = new String(); 

function FindFile(FOo) 
{ 
var FSo = new Enumerator(FOo.Files); 
for(i=0;!FSo.atEnd();FSo.moveNext()) 
{ 
    if(FileName == "*" ||  FSo.item().name.slice(0,FSo.item().name.lastIndexOf(".")).toLowerCase().indexOf(FileName)>-1) 
     if(Extention == "*" || FSo.item().name.slice(FSo.item().name.lastIndexOf(".")+1).toLowerCase().indexOf(Extention)>-1){ 
      StrOut += "<tr "+ ((i%2)? "":"bgcolor=#DDAA55") +"><td width=50%><font class=find>" + FSo.item().name + "</font></td><td width=25%><font class=find>" + FSo.item().type + "</font></td><td width=50%><font class=find>"+ String(FSo.item().size/(1024*1024)).slice(0,3) +" MB</font></td></tr>"; 
      i++ 
      } 
} 
} 

function Scan() 
{ 
FileName = (search.value.lastIndexOf(".")>-1)? search.value.slice(0,search.value.lastIndexOf(".")):(search.value.length>0)? search.value.toLowerCase():"*"; //Get Searched File Name 
Extention = (search.value.lastIndexOf(".")>-1)? search.value.slice(search.value.lastIndexOf(".")+1).toLowerCase():"*"; // Get Searched File Extention Name 

if(path.value.length>0 && Fo.FolderExists(path.value)){ 
    StrOut = "<table border=0 width=100% cellspacing=0>" 
    FindFile(Fo.GetFolder(path.value)); 
    outPut.innerHTML = StrOut+"</table>"; 
    } 
else alert("Insert Correct Path Address"); 
} 
</script> 

<BODY topmargin="0" leftmargin="0"> 

<table border=0 width=100% cellspacing="0" style="border-collapse: collapse" cellpadding="2"><tr> 
<td dir="ltr" bgcolor="#FFCC00"><b><font face="Arial" size="2">Named : 
</font></b> </td> 
<td dir="ltr" bgcolor="#FFCC00"> 
<input size=50 type=text id=search name=search class="Field"></td> 
</tr><tr> 
<td dir="ltr" bgcolor="#FFCC00"> 
<p dir="ltr"><b><font face="Arial" size="2">Path : </font></b> </td> 
<td bgcolor="#FFCC00"> 
<input size=50 type=text value="C:\" id=path name=path class="Field" ></td> 
</tr><tr> 
<td bgcolor="#FFCC00">&nbsp;</td> 
<td bgcolor="#FFCC00"> 
<input type=button value="  Scan   " onclick=Scan() class="Field"></td> 
</tr><tr> 
<td colspan=2 align=right bgcolor="#FFCC00"><font face=arial size=2><b>Search Result</b></font><hr></td> 
</tr><tr> 
<td colspan=2 bgcolor="#FFCC00"><div id=outPut></div></td> 
</tr></table> 
</BODY> 
</HTML> 
関連する問題