如何在C#中创建文件目录? [英] How Can I Create File Directory In C#?

查看:138
本文介绍了如何在C#中创建文件目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有几个关于以下文件目录概念的dout,用于创建文件目录。如何使用文件目录创建文件夹。我已经完成的代码但是我收到了错误,请看下面我的代码,让我知道我在哪里做错了。我想创建如下文件夹,



mch \\Popages \\Terms \\clientname \\

---> clientname文件夹来自我的班级文件

请参考下面的代码

代码是:

  protected   void  btnUpload_Click1( object  sender,EventArgs e)
{
try
{
// if(ASBsupplier.Text ==)
// {
// lblMessage.Text = 选择供应商;
// return;
< span class =code-comment> // }

if (FileUpload1.PostedFile.ContentLength == 0
throw new ApplicationException( 选择有效文件);

if (!Directory.Exists(Server.MapPath( ..)+ \\mch \\\ \\ popages \\Terms \\ClientName \\))
{
client = Helper.GetParentFolderName()。ToString();
Directory.CreateDirectory(Server.MapPath( ..)+ \\mch \\popages \\Terms \\ + client + );
// 此目录无法创建。
}

FileName = FileUpload1.FileName;
d_SelectedFileSize = FileUpload1.PostedFile.ContentLength / 1024 ;

if (d_SelectedFileSize > 25
{
lblMessage.Text = 文件大小超过最大限制25 kb;
return ;
}


if + rbtnFormat.SelectedValue == Path.GetExtension(FileName))
{
client = Helper.GetParentFolderName()。ToString();

FilePath = Server.MapPath( ..)+ \\mch \\popages \\Terms \\ + client + < span class =code-string> \\ + ASBsupplier.Text + Path.GetExtension(FileName)+ ;

if (System.IO.File.Exists(FilePath))
{
try
{
System.IO.File.Delete(FilePath);
}
catch (System.IO.IOException er)
{
呃;
}
}

if (!File.Exists(FilePath))
{
FileUpload1.PostedFile.SaveAs(FilePath);
lblMessage.Text = 文件已成功保存;
}
}
else
{
throw new ApplicationException( 上传的文件格式不匹配选择的文件格式);
}
}
catch (例外情况)
{
lblMessage.Text = ex.Message;
}
}



提前感谢..

解决方案

不,你无法在您网站的根目录上创建文件或文件夹 - 这就是..指定的内容。 是当前文件夹,..是上面的文件夹。并且您没有IIS授予的权限来访问您的网站结构之外的目录。

尝试在您的站点文件夹下创建它: Server.MapPath(〜)

Hi All,
i have a few douts about the below file directory concept, for creating file directory. how can i create folder using file directory. the code i have done but am getting the error, please have a look my code below and let me know where i have done a mistake. i want to create folder like below,

mch\\popages\\Terms\\clientname\\
--->clientname folder which is come from my class file
please refer my code below
code is :

protected void btnUpload_Click1(object sender, EventArgs e)
{
    try
    {
        //if (ASBsupplier.Text == "")
        //{
        //    lblMessage.Text = "Select Supplier";
        //    return;
        //}

        if (FileUpload1.PostedFile.ContentLength == 0)
            throw new ApplicationException("Select valid file");

        if (!Directory.Exists(Server.MapPath("..") + "\\mch\\popages\\Terms\\ClientName\\"))
        {
            client = Helper.GetParentFolderName().ToString();
            Directory.CreateDirectory(Server.MapPath("..") + "\\mch\\popages\\Terms\\" + client + "");
// this directory cannot create.           
        }

        FileName = FileUpload1.FileName;
        d_SelectedFileSize = FileUpload1.PostedFile.ContentLength / 1024;

        if (d_SelectedFileSize > 25)
        {
            lblMessage.Text = "File size exceeds maximum limit 25 kb";
            return;
        }


        if ("." + rbtnFormat.SelectedValue == Path.GetExtension(FileName))
        {
            client = Helper.GetParentFolderName().ToString();
    
            FilePath = Server.MapPath("..") + "\\mch\\popages\\Terms\\"+client+"\\" + ASBsupplier.Text + Path.GetExtension(FileName) + "";

            if (System.IO.File.Exists(FilePath))
            {
                try
                {
                    System.IO.File.Delete(FilePath);
                }
                catch (System.IO.IOException er)
                {
                    throw er;
                }
            }

            if (!File.Exists(FilePath))
            {
                FileUpload1.PostedFile.SaveAs(FilePath);
                lblMessage.Text = "File saved successfully";
            }
        }
        else
        {
            throw new ApplicationException("Uploaded file format not matched with selected file format");
        }
    }
    catch (Exception ex)
    {
        lblMessage.Text = ex.Message;
    }
}


thanks in advance..

解决方案

No, you can't create files or folders above the root directory for your web site - and that's what ".." specifies. "." is the current folder, ".." is the folder above that. And you do not have permission granted by IIS to access directories outside your web site structure.
Try creating it under your site folder instead: Server.MapPath("~")


这篇关于如何在C#中创建文件目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆