ASP.NET - how to create folder dynamically and upload zip file from treeview

Asked By mani on 30-Jan-12 01:55 AM
hi..i have treeview and fileupload controll in my page..now i need to create folders dynamically and upload zip file..i have to delete and rename from selected folder in treeview also...any help???
Web Star replied to mani on 30-Jan-12 01:58 AM
you just use System.IO namespace and than specify the correct path where you want to create folder and use CreateDirectory method of Directory class as follows


string pathToCreate = "~/UserFolders/" + TextBox1.Text;
if(Directory.Exists(Server.MapPath(pathToCreate))
{
   //In here, start looping and modify the path to create to add a number
   //until you get the value needed
}


//Now you know it is ok, create it
Directory.CreateDirectory(Server.MapPath(pathToCreate));

and for upload zip file simply use Upload control and save posted file from that control to specific folder which you created
smr replied to mani on 30-Jan-12 01:59 AM
hi


refer this example

<%@ Page Language="VB" %>
<script runat="server">
  
  Protected Sub btnFileUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    If myFileUpload.HasFile Then
      myFileUpload.SaveAs(Server.MapPath("uploads/" & myFileUpload.FileName))
    End If
  End Sub
  
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>ASP.NET 2.0 FileUpload Control Sample</title>
</head>
<body>
  <form id="myForm" runat="server">
    <asp:FileUpload ID="myFileUpload" runat="server" />
    <asp:Button ID="btnFileUpload" runat="server"
      Text="Upload File"
      OnClick="btnFileUpload_Click"
    />
  </form>
</body>
</html> 


follow
http://www.eggheadcafe.com/searchform.aspx?search=upload+zip+file+in+asp.net+1.1
http://forums.asp.net/t/1643159.aspx
D Company replied to mani on 30-Jan-12 02:00 AM
hello Friend.

If you want to change the text (rename) of the selected node use:

TreeView1.SelectedNode.Text = "Your new text"

or

You call BeginEdit on the node. Also make sure that the treeview has
LabelEdit set to True

or



 //formats the Text of the menu item
        function formatMenuItem(menuItem, treeNode, formatString)
        {
          var nodeValue = treeNode.get_value();
          if (nodeValue && nodeValue.indexOf("_Private_") == 0)
          {
            menuItem.set_enabled(false);
          }
          else
          {
            menuItem.set_enabled(true);
          }
          var newText = String.format(formatString, extractTitleWithoutMails(treeNode));
          menuItem.set_text(newText);
        }



// to remove

TreeView1.SelectedNode.Remove();

Regards
D
Jitendra Faye replied to mani on 30-Jan-12 02:00 AM
To create folder  use this code-

 try
      {
        // Determine whether the directory exists.
        if (Directory.Exists(path))
        {
          Console.WriteLine("That path exists already.");
          return;
        }

        // Try to create the directory.
        DirectoryInfo di = Directory.CreateDirectory(path);
        Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path));
     
      }
      catch (Exception e)
      {
        Console.WriteLine("The process failed: {0}", e.ToString());
      }

Try this and let me know.

smr replied to mani on 30-Jan-12 02:01 AM
hi

You can use below code to create a folder. But, you need to have enough privileges (impersonation) on the server to create folder

using System.IO;
DirectoryInfo _DicInfo = new DirectoryInfo(@"C:\NewFolder");
if (!_DicInfo.Exists)
{
Directory.CreateDirectory(@"C:\NewFolder");
}

smr replied to mani on 30-Jan-12 02:02 AM
hi

You can use below code to create a folder. But, you need to have enough privileges (impersonation) on the server to create folder

using System.IO;
DirectoryInfo _DicInfo = new DirectoryInfo(@"C:\NewFolder");
if (!_DicInfo.Exists)
{
Directory.CreateDirectory(@"C:\NewFolder");
}

smr replied to mani on 30-Jan-12 02:06 AM

hi

You can use below code to create a folder. But, you need to have enough privileges (impersonation) on the server to create folder

using System.IO;
DirectoryInfo _DicInfo = new DirectoryInfo(@"C:\NewFolder");
if (!_DicInfo.Exists)
{
Directory.CreateDirectory(@"C:\NewFolder");
}

smr replied to mani on 30-Jan-12 02:06 AM

hi

You can use below code to create a folder. But, you need to have enough privileges (impersonation) on the server to create folder

using System.IO;
DirectoryInfo _DicInfo = new DirectoryInfo(@"C:\NewFolder");
if (!_DicInfo.Exists)
{
Directory.CreateDirectory(@"C:\NewFolder");
}

Jitendra Faye replied to mani on 30-Jan-12 02:09 AM
To Zip/UnZip file use this code-

http://www.eggheadcafe.com/community/csharp/2/10401515/how-to-extract-zip-file-using-c.aspx

 

This use code-

A short alternative is ZipStorer (http://zipstorer.codeplex.com/). You don't need to use an external library (.DLL) as it is implemented in one .cs source file.http://zipstorer.codeplex.com/

Extraction example:

// Open an existing zip file for reading
using (ZipStorer zip = ZipStorer.Open(@"c:\data\sample.zip", FileAccesss.Read))
{
// Read the central directory collection
List<ZipStorer.ZipFileEntry> dir = zip.ReadCentralDir();

// Look for the desired file
foreach (ZipStorer.ZipFileEntry entry in dir)
{
if (Path.GetFileName(entry.FilenameInZip) == "sample.jpg")
{
// File found, extract it
zip.ExtractStoredFile(entry, @"c:\data\sample.jpg");
break;
}
}
}

Try this and let me know.

 
kalpana aparnathi replied to mani on 30-Jan-12 03:37 AM
create folder dynamically
 
public partial class _Default : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    CreateTreeView();
  }
 
  public void CreateTreeView()
  {
    TreeView tv = new TreeView();
 
    DirectoryInfo dir = new DirectoryInfo("E:\\way backup");//give path of Root directory
    TreeNode tnRoot = new TreeNode();
    tnRoot.Text = dir.Name;
    tnRoot.Value = dir.FullName;
    tv.Nodes.Add(tnRoot);
 
    AddDirs(dir, tnRoot);
 
    tv.SelectedNodeChanged += new EventHandler(tv_SelectedNodeChanged);
 
    PlaceHolder1.Controls.Add(tv);
  }
 
  void tv_SelectedNodeChanged(object sender, EventArgs e)
  {
    TreeView tv = (TreeView)sender;
    Response.Write(tv.SelectedNode.Value);
  }
 
  public void AddDirs(DirectoryInfo dir, TreeNode parent)
  {
    DirectoryInfo[] dirs = dir.GetDirectories();
 
    foreach (DirectoryInfo d in dirs)
    {
      TreeNode tn = new TreeNode();
      tn.Text = d.Name;
      tn.Value = d.FullName;
      parent.ChildNodes.Add(tn);
      AddDirs(d, tn);
    }
  }
}

public void GetDir_files(string sourceDir)
  {
    Response.Write(sourceDir + "<br /><br />");
      
    string[] fileEntries = Directory.GetFiles(sourceDir);
    foreach (string fileName in fileEntries)
    {
      // do something with fileName
      Response.Write(fileName + "<br />");
    }
 
    // Recurse into subdirectories of this directory.
    string[] subdirEntries = Directory.GetDirectories(sourceDir);
    foreach (string subdir in subdirEntries)      
      if ((File.GetAttributes(subdir) & FileAttributes.ReparsePoint) !=
           FileAttributes.ReparsePoint)
        GetDir_files (subdir);
}


for more detail:http://demos.telerik.com/aspnet-ajax/treeview/examples/functionality/contextmenu/defaultcs.aspx