2011-08-03 324 views
0

我已經寫了從特定的URL下載文件的代碼,但有錯誤「System.UnauthorizedAccessException:對路徑」。這裏下面的代碼遇到錯誤:System.UnauthorizedAccessException:對路徑C:

  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服務器..

+1

您是否擁有對C:\ Demo_Dir \ HQ_Server \ Xerox-RTBI協議的寫入權限? –

+1

更好地引用完整的錯誤消息,並檢查您的代碼和本文中的路徑字符串是如何格式化的。 –

回答

0

首先,它應該是:

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

第二,你必須通過右鍵單擊它在Windows中授予了Demo_Dir目錄權限ASP.NET和/或IUSR用戶帳戶資源管理器 - >安全 - >添加

我寫了small tutorial解釋瞭如何授予權限,因爲您需要對要寫入的目錄有權限,所以它也與您的情況相關。

+0

我沒有使用asp.net。使用visual c#windows窗體 – soundy

+0

嘗試使用這樣的代碼:'File.WriteAllText(@「C:\ Demo_Dir \ HQ_Server \ Xerox-RTBI Agreement \ samlpe.txt」,「hello world」);' - 正在創建的文件? –

+0

如果你省略'@'它甚至不應該編譯。我認爲OP在他的原始代碼中有'\\\'。 –

相關問題