简体   繁体   中英

Code crashing Cassini web server

Just a simple crap code. I confused with the syntax when I tried to execute that code by clicking on button, page is trying to load but it couldn't. I tried to load the same page in multiple tabs and I got error "Web server stopped working"! Can anybody correct the syntax of following simple code?

    string folderpath = @"C:\Users\Nouser\Documents\Visual Studio 2010\WebSites\folders";
    string foldername = TextBox1.Text;
    string newPath = System.IO.Path.Combine(folderpath, foldername);
    while (Directory.Exists(newPath))
    {
        foldername = foldername + ik;
        ik = ik + 1;
    }
    System.IO.Directory.CreateDirectory(newPath);

I'm pretty sure that the check Directory.Exists(newPath) is only ever evaluating the initial value set to newPath, so causing an endless loop.

Debug the loop by stepping through and seeing what newPath is set as upon each iteration.

I think what you want is something more like this:

int ik = 1;
string folderpath = @"C:\Users\Nouser\Documents\Visual Studio 2010\WebSites\folders";
string foldername = TextBox1.Text;
string newPath = System.IO.Path.Combine(folderpath, foldername);
while (Directory.Exists(newPath))
{
    newPath = System.IO.Path.Combine(folderpath, foldername + ik);
    ik = ik + 1;
}
System.IO.Directory.CreateDirectory(newPath);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM