Heena
Heena

Reputation: 754

how to create folder on server from code in c#?

Is there any way to show user popup to give the name to create folder

And with that if he does not want to create folder then he should be shown the list of folders to which he want to save the file

Is it proper way that I create a popup with javascript with the list of folders on my server and put a textbox and button to save the new and in code we will create directory in code passed with textbox?

Upvotes: 0

Views: 21509

Answers (7)

kazem tnt
kazem tnt

Reputation: 41

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));

Upvotes: 4

Shahid Amin
Shahid Amin

Reputation: 231

                        string targetPath = Server.MapPath("FolderName"); //with complete path
                        if (!System.IO.Directory.Exists(targetPath))
                        {
                            System.IO.Directory.CreateDirectory(targetPath);                               
                            fileToUpload.PostedFile.SaveAs(targetPath + ImageFileName);
                        }
                        else
                        {
                            fileToUpload.PostedFile.SaveAs(targetPath + ImageFileName);
                        }

Upvotes: 2

mtazva
mtazva

Reputation: 1015

Web applications and the HTTP protocol do not support direct manipulation of the server-side file system. In fact, that would be a dangerous idea.

It sounds like you have a base folder somewhere on the web server where you want users to be able to upload files, with some user control over subfolders and location of the uploaded file. I would suggest dong this by presenting the file system in a tree view or a list of links (where the links let you navigate up/down the folder tree). Combine this with a file input and you've got yourself a solution. HOWEVER, be very careful to control and cleanse the specified file name for the uploaded file. There are many combinations (such as utilizing "..") that can allow the user to hack into unwanted file locations on your server.

Upvotes: 0

Waqas Raja
Waqas Raja

Reputation: 10872

As I mentioned by commenting that Functionality like SaveFileDialog is only possible in Windows Forms application, you cannot do this in asp.net

The alternatives in asp.net are difficult however as an alternate there is a third party editor available on the web called CKEditor 3.x Developer's Guide.

You can use File Browser (Uploader) for the purpose you want to achieve.

Upvotes: 0

Using the Example given in How to Create a Folder or File You can create a folder, but in asp.net you can not Use open file dialog or save file dialog functionalities, this will work only in C# Windows forms application.

Upvotes: 0

Alim Ul Gias
Alim Ul Gias

Reputation: 6791

If you are developing web applications (I can see you have tagged your question as asp.net), you do not need to worry about the pop up window. You just send the file through the Response and the download window will be shown by the browser.

Upvotes: 0

deepi
deepi

Reputation: 1081

to create folder :

 System.IO.Directory.CreateDirectory(newPath);//newpath is your folder creating path

Upvotes: 0

Related Questions