Tvd
Tvd

Reputation: 4601

Trying to Create a Folder, but it creates in different location

In my ASP.NET WEbForms app, I want to create a folder and save files in it. In my project I have a folder named CRMImages/Projects. I want to create a sub folder in Projects folder & save images from their. Currently I retrieve images from CRMImages/ as the parent folder.

This is my code that I have on code-behind :

                try
                {
                    string pathToCreate = "~/CRMImages/Projects/" + item.ProjectId;
                    string myFileName = "";
                    if (!Directory.Exists(Server.MapPath(pathToCreate)))
                    {
                        DirectoryInfo di = Directory.CreateDirectory(pathToCreate);
                        var user = System.Security.Principal.WindowsIdentity.GetCurrent().User;
                        var userName = user.Translate(typeof(System.Security.Principal.NTAccount));
                        System.Security.AccessControl.DirectorySecurity sec = di.GetAccessControl();
                        sec.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule(userName,
                            System.Security.AccessControl.FileSystemRights.Modify,
                            System.Security.AccessControl.AccessControlType.Allow));
                        di.SetAccessControl(sec);
                        Directory.CreateDirectory(pathToCreate);

                        System.Diagnostics.Debug.WriteLine("FOLDER CREATED PATH : " + di.FullName);
                        myFileName = pathToCreate + "/projectLogo.png";
                        System.Diagnostics.Debug.WriteLine("PATH To Save Logo File & NAME : " + myFileName);

                        /*
                        if (File.Exists(item.ProjectLogoUrl) ) {
                            FileUpload projLogoUpload = new FileUpload();
                            if (projLogoUpload.HasFile) {
                                myFileName = pathToCreate + "/projectLogo.png";
                                projLogoUpload.SaveAs(myFileName);
                            }
                            // panFileBtn.SaveAs(filePath);
                        }  */
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("EXCEPTION While SAving File : " + ex.Message + "\n *** STACK" + ex.StackTrace);
                }  

The code executes, but I don't see the folder created in my project folder. Lets say the value of item.ProjectId is "EMP3", the logs that I see on the above code execution is :

FOLDER CREATED PATH : C:\Program Files (x86)\IIS Express\~\CRMImages\Projects\EMP3
PATH To Save Logo File & NAME : ~/CRMImages/Projects/EMP3/projectLogo.png

I checked in IIS Express folder & there this full path is created. Can you say why is it saving in IISExpress & how to create the folder in the /CRMImages/Projects folder that already exists in my project !!

Any help is highly appreciated. Thanks

Upvotes: 1

Views: 35

Answers (1)

Keval Gangani
Keval Gangani

Reputation: 1328

You have to replace below line

DirectoryInfo di = Directory.CreateDirectory(pathToCreate);

With

DirectoryInfo di = Directory.CreateDirectory(Server.MapPath(pathToCreate));

Upvotes: 1

Related Questions