简体   繁体   中英

Access to the path is denied. in c#

im programming a application with c# and now im in trouble to build and write a.txt file with this error: Access to the path 'E:\compex\Thursday, October 10, 2019' is denied. and my related code is it:

 private void creat_Click(object sender, EventArgs e)
    {
        string filename = "E:\\compex\\"+DateTime.Now.ToLongDateString() ;
        string msadd = filename + "\\msadd.txt";
        textpatch.Text = msadd;
        Directory.CreateDirectory(filename);
        filepatch.Text =  filename;
        using(FileStream fp = File.Create(filename))
        {
            log.Text = "address file created successfully";
            Byte[] filepatchs = new UTF8Encoding(true).GetBytes(filename);
            fp.Write(filepatchs, 0, filepatchs.Length);
            log.Text = "";
            log.Text = "address  successfully";
        } 



    }

whats wrong with it? is there any permission in windows or code using to get that?

private void creat_Click(object sender, EventArgs e)
{
        string filename = "E:\\compex\\"+DateTime.Now.ToLongDateString() ;
        string msadd = filename + "\\msadd.txt";
        textpatch.Text = msadd;
        Directory.CreateDirectory(filename);
        filepatch.Text =  filename;
        using(FileStream fp = File.Create(msadd))
        {
            log.Text = "address file created successfully";
            Byte[] filepatchs = new UTF8Encoding(true).GetBytes(filename);
            fp.Write(filepatchs, 0, filepatchs.Length);
            log.Text = "";
            log.Text = "address  successfully";
        } 



}

I believe, basing on your comment reply, that you meant to use msadd as the concatenated filename to write to in your using block, instead of using filename duplicated for both the directory and filename.

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