Creating folder in a server

4 posts, 0 answers
  1. Jols
    Jols avatar
    69 posts
    Member since:
    Mar 2009

    Posted 25 Apr 2009 Link to this post

    hello to expert in telerik
     
                          i apologize if i go this room, i just to want ask for a help for you guys who knows very well on asp.net, my question is how am i going to create a folder on the server base on the person who login("the name of the folder create will be on the name who login on the page".) on the system and then when that user again login on the system and access his attachment file he get it.. like for example

    i login on the system name: Jolan, now i click the button attached file which actully the browse files, now i will click the save  button to save the browse files to folder created as my attached file to database,. those login name will be created as a folder name on the server..  how am i going to do that..

    sample/codes is highly appreciated
  2. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 26 Apr 2009 Link to this post

    Hi Jols,

    expecting that you don't use impersonation (an advanced technic) you will see that "NT-authority\networkservice"
    --I only guess the name since my OS is german where it is named "NT-Autorität\Netzwerkdienst"

    So the first thing to do is to create a folder in your webapp let's call it "UserDirs".
    You can do this in VS by Add new folder. And this folder will be the one where the directories of the users will be created.
    Next you have to enable the ASP.NET service account the rights to create files / folders there (which is not default).
    So choose your folder on the server (with explorer) and select security - add the account (shown above) and give it "Full access" to this folder.
    Now the system would work from the perspective of the rights.
    Another important thing - assure that the usernames fit the rules for directory / filenames!!
    Not + \ and other prohibited letters in at. And it must also fit the HTTP rules for filenames no / : and so forth.

    Next we have to face the fact, that the webserver addresses files with an HTTP URL while file operations need "file system path's".
    So http://www.mydom.com/UserDirs/Franz can be something like c:\iisroot\MyWeb\UserDirs\Franz

    Since I'm not sure what you mean with "as my attached file to database" I reduce this to a textfile I'll create for a specific user.
    And I assume a function "after login" - which does:
    a.) tries to find the file - and add it as an href to a link in the page.
    b.) if the file / folder is not found I create both
    c.) as "private file" I statically use the name private.txt

    Since I don't want implement user auth for this sample I use the following form:
    <div> 
    Username: <asp:TextBox ID="txtOne" runat="server" /><br /> 
    <asp:HyperLink runat="server" ID="hlPrivateFile">Your private file</asp:HyperLink><br /> 
    Simulate Login: <asp:Button ID="btnGO" runat="server" Text="GO" onclick="btnGO_Click" /> 
    </div> 
     
    txtOne is the control where you must enter the username - the GO button simulates the login.
    In code behind I check if there is a valid username - if not I simply return.
    If there is a username - I do what you asked - check for a dir / create one if needed.....

    The basic code behind looks like this:
    protected void Page_Load(object sender, EventArgs e) {  
        hlPrivateFile.Visible = false;  //only visible if we have done everything  
    }  
    protected void btnGO_Click(object sender, EventArgs e) {  
        if (string.IsNullOrEmpty(txtOne.Text)) {    // no username - do nothing  
            return;  
        }  
        HandleUserFile(txtOne.Text);    // here we will do the things  
    }  
     
    private void HandleUserFile(string strUserName) {  
        //just to see if it works (for the moment)  
        hlPrivateFile.NavigateUrl = "/UserFiles/" + strUserName + "/private.txt";  
        hlPrivateFile.Text = "/UserFiles/" + strUserName + "/private.txt";  
        hlPrivateFile.Visible = true;  
    }  
     
    You can create a new ASPX page - copy those things in - and check what it does.

    Now it is all about "HandleUserFile".
    The very first thing - we need the file system path for the "web dir" /UserFiles.
    Than we check if the user directory exists -  if not we create it (and also the file in it):
            private void HandleUserFile(string strUserName) {  
                string strFileDir = Server.MapPath("/UserFiles");   //get the file system path - will give something like c:\iisroot\myweb\userfiles  
                string strDir4ThisUser = System.IO.Path.Combine(strFileDir, strUserName);   //will give something like c:\iisroot\myweb\userfiles\John  
                if (!System.IO.Directory.Exists(strDir4ThisUser)) { //directory does not exist!!  
                    System.IO.Directory.CreateDirectory(strDir4ThisUser);   //create it  
                    CreateInitialFile(strDir4ThisUser); //new dir created - we also must create a new file there  
                }  
     
                //just to see if it works (for the moment)  
                hlPrivateFile.NavigateUrl = "/UserFiles/" + strUserName + "/private.txt";  
                hlPrivateFile.Text = "/UserFiles/" + strUserName + "/private.txt";  
                hlPrivateFile.Visible = true;  
            }  
     
    The last step is to bring the file to that directory - or in this case - simply create a textfile in the new directory:
    private void CreateInitialFile(string strDir4ThisUser) {  
        System.IO.StreamWriter sW = new System.IO.StreamWriter(strDir4ThisUser + "/private.txt");  
        sW.WriteLine("Testfile");  
        sW.WriteLine("Just a sample");  
        sW.Close();  
        sW.Dispose();  
          
    }  
     
    That's it - the previously implemented code to set the hyperlink it still there
              //just to see if it works (for the moment)     
                hlPrivateFile.NavigateUrl = "/UserFiles/" + strUserName + "/private.txt";     
                hlPrivateFile.Text = "/UserFiles/" + strUserName + "/private.txt";     
                hlPrivateFile.Visible = true;     
     
    - so now we can follow the hyperlink an it will show the file.

    HTH

    Manfred


  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jols
    Jols avatar
    69 posts
    Member since:
    Mar 2009

    Posted 27 Apr 2009 Link to this post

    sir this is what i've done with your codes, but my problem is i having  an error about this seconds codes that i created the createfiles..please revised if i missed something or its really wrong..

    here's the codes

      #region"Create Directory and files with it"
            private void HandleUserDirectory(string strUserName)
            {
                string strFileDir = Server.MapPath("~/CHS/Files/");
                string strDir4ThisUser = System.IO.Path.Combine(strFileDir, strUserName);  
                if (!System.IO.Directory.Exists(strDir4ThisUser))
                {
                    //directory does not exist!!   
                    System.IO.Directory.CreateDirectory(strDir4ThisUser);   //create it   
                    Createfiles(strDir4ThisUser);
                }
            }

            //creare files
            private void Createfiles(string strDir4ThisUser)
            {
                if (this.FileUpload1.FileName != "")
                {
                    this.FileUpload1.PostedFile.SaveAs(Server.MapPath(System.IO.Path.Combine(strDir4ThisUser,FileUpload1.FileName)));
                }
            }
            #endregion

    sample/codes si highly appreciated
  5. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 08 May 2009 Link to this post

    Hi Jols,

    could you be a bit more specific about "having an error"?
Back to Top