代码崩溃Cassini Web服务器

时间:2012-01-21 16:19:24

标签: c# while-loop

只是一个简单的垃圾代码。当我尝试通过单击按钮执行该代码时,我混淆了语法,页面正在尝试加载,但它无法加载。我试图在多个选项卡中加载相同的页面,我收到错误“Web服务器停止工作”!任何人都可以纠正以下简单代码的语法吗?

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

2 个答案:

答案 0 :(得分:3)

我很确定检查Directory.Exists(newPath)只是评估设置为newPath的初始值,因此导致无限循环。

通过逐步调试并查看每次迭代时设置的newPath来调试循环。

答案 1 :(得分:1)

我认为你想要的更像是这样:

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