开发者

Got error: system.unauthorizedAccessException: Access to the path c:

I've written code for download file from specific url, but got error 'system.unauthorizedAccessException: Access to the path'..here following code

            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.Data;
            using System.Drawing;
            using System.Linq;
            using System.Text;
            using System.Windows.Forms;
            using System.Net;
            using System.IO;

            namespace WindowsFormsApplication1
            {
             public partial class Form1 : Form
             {
               public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        UploadFile(textBox2.Text, textBox1.Text);
    }

    private void button2_Click(object sender, EventArgs e)
    {

    }

    public void UploadFile(string localFile, string uploadUrl)
    {
        try
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uploadUrl);
            req.Method = "PUT";
            req.AllowWriteStreamBuffering = true;

            // Retrieve request stream and wrap in StreamWriter
            Stream reqStream = req.GetRequestStream();
            StreamWriter wrtr = new StreamWriter(reqStream);

            // Open the local file
            StreamReader rdr = new StreamReader(localFile);

            // loop through the local file reading each line 
            //  and writing to the request stream buffer
            string inLine = rdr.ReadLine();
            while (inLine != null)
            {
                wrtr.WriteLine(inLine);
                inLine = rdr.ReadLine();
            }

            rdr.Close();
            wrtr.Close();

            req.GetResponse();
        }
        catch (Exception er)
        {
            MessageBox.Show(er.ToString());
        }
    }

    private void button2_Click_1(object sender, EventArgs e)
    {
        DownloadFile(textBox2.Text, textBox1.Text);
    }


    public void DownloadFile(string localFile, string downloadUrl)
    {
        try
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(downloadUrl);
            req.Method = "GET";

            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

            // Retrieve response stream and wrap in StreamReader
            Stream respStream = resp.GetResponseStream();
            StreamReader rdr = new StreamReader(respStream);

            // Create the local file
            StreamWriter wrtr = new StreamWriter(localFile);

            // loop through response stream reading each line 
            //  and writing to the local file
            string inLine = rdr.ReadLine();
            while (inLine != null)
            {
                wrtr.WriteLine(inLine);
                inLine = rdr.ReadLine();
            }

            rdr.Close();
            wrtr.Close();
        }
        catch (Exception er)
        {
            MessageBox.Show(er.ToString());
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        DownloadFileBinary(textBox2.Text, textBox1.Text);
    }

    public void DownloadFileBinary(string localFile, string downloadUrl)
    {
        try
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(downloadUrl);
            req.Method = "GET";

            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

            // Retrieve response stream
            Stream respStream = resp.GetResponseStream();

            // Create local file
            //string localFilename = "@" + localFile;
            FileStream wrtr = new FileStream(localFile, FileMode.Create);

            // Allocate byte buffer to hold stream contents
            byte[] inData = new byte[4096];

            // loop through response stream reading each data block
            //  and writing to the local file
            int bytesRead = respStream.Read(inData, 0, inData.Length);
            while (bytesRead > 0)
            {
                wrtr.Write(inData, 0, bytesRead);
                bytesRead = respStream.Read(inData, 0, inData.Length);
            }

            respStre开发者_运维百科am.Close();
            wrtr.Close();
        }
        catch (Exception er)
        {
            MessageBox.Show(er.ToString());
        }
    }


}

}

this my complete code. here using apache tomcat server ..


First, it should be:

FileStream wrtr = new FileStream(@"C:\Demo_Dir\HQ_Server\Xerox-RTBI Agreement\samlpe.docx", FileMode.Create);

And second, you have to grant permissions to the ASP.NET and/or IUSR user accounts over Demo_Dir directory by right clicking it in Windows Explorer --> Security --> Add

I have written small tutorial explaining how to grant permissions, it's also relevant in your case as you need the permissions over the directory you want to write into.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜