收到错误:system.unauthorizedAccessException:访问路径c:

时间:2011-08-03 07:03:49

标签: c# http

我已经从特定网址编写了下载文件的代码,但是收到错误'system.unauthorizedAccessException:访问路径'..这里是代码

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

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


}

}

这是我的完整代码。这里使用apache tomcat server ..

1 个答案:

答案 0 :(得分:0)

首先,它应该是:

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

其次,您必须通过在Windows资源管理器中右键单击ASP.NET和/或IUSR用户帐户,在Demo_Dir目录上授予权限 - >安全 - >添加

我已经写了small tutorial来解释如何授予权限,它在您的情况下也是相关的,因为您需要对要写入的目录的权限。