是webform还是winform,如果是webform建议使用HttpPostedFile.SaveAs 方法
String TempFileName;
 HttpFileCollection MyFileCollection = Request.Files;
 
 for (int Loop1 = 0; Loop1 < MyFileCollection.Count; Loop1++)
 {
    // Create a new file name.
    TempFileName = "C:\\TempFiles\\File_" + Loop1.ToString();
    // Save the file.
    MyFileCollection[Loop1].SaveAs(TempFileName);
 }
不过只能在程序所在的服务器上运行。
如果按你的程序可能还需要将程序所在的主机添加到目标主机 WebPermission 的“连接”和“接受”列表

解决方案 »

  1.   

    是webform,我知道你说的那个方法,
    谢谢,你的帮助。
    请问有没有熟悉我说的这种方法的。
      

  2.   

    在服务器端要有相应处理上传文件的程序或页面。这个方法只是将文件以字节数组形式发送给服务器等待服务器处理,URI参数要有实际的处理页面;
    也可以指定发送数据的方式
    myWebClient.UploadFile(uriString,"POST",fileName);
      

  3.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Net;
    using System.Text;
    using System.IO;
    using mshtml;
    namespace UploadFile
    {
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label1;
        private AxSHDocVw.AxWebBrowser axWebBrowser1;

    private System.ComponentModel.Container components = null; public Form1()
    {
    InitializeComponent();
    }
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    private void InitializeComponent()
    {
          System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
          this.label1 = new System.Windows.Forms.Label();
          this.axWebBrowser1 = new AxSHDocVw.AxWebBrowser();
          ((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).BeginInit();
          this.SuspendLayout();
          // 
          // label1
          // 
          this.label1.Location = new System.Drawing.Point(8, 8);
          this.label1.Name = "label1";
          this.label1.Size = new System.Drawing.Size(456, 16);
          this.label1.TabIndex = 0;
          this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
          // 
          // axWebBrowser1
          // 
          this.axWebBrowser1.Enabled = true;
          this.axWebBrowser1.Location = new System.Drawing.Point(8, 24);
          this.axWebBrowser1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxState")));
          this.axWebBrowser1.Size = new System.Drawing.Size(456, 224);
          this.axWebBrowser1.TabIndex = 1;
          // 
          // Form1
          // 
          this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
          this.ClientSize = new System.Drawing.Size(472, 253);
          this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                      this.axWebBrowser1,
                                                                      this.label1});
          this.Name = "Form1";
          this.Text = "Form1";
          this.Load += new System.EventHandler(this.Form1_Load);
          ((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).EndInit();
          this.ResumeLayout(false);    }
    #endregion
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    }    private void Form1_Load(object sender, System.EventArgs e)
        {
          // 需要注意的是:Images文件夹有匿名可写的权限。
          // xx.xml为新文件名字
          string uriString = "http://mengxianhui/aspxWeb/Images/xx.txt";      // 创建WebClient实例
          WebClient myWebClient = new WebClient();
          // 要上传的文件
          string fileName = "c:\\book.txt";
          myWebClient.Headers.Add("Content-Tyep","application/x-www-form-urlencoded");
          try
          {
            myWebClient.UploadFile(uriString,fileName);
            label1.Text = "上传成功!";
            object Zero = 0;
            object EmptyString = "";
            axWebBrowser1.Navigate(uriString,ref Zero, ref EmptyString, ref EmptyString, ref EmptyString);
          }
          catch(WebException xx)
          {
            label1.Text="上传失败:" + xx.Message;
          }
        }
    }
    }
      

  4.   

    myWebClient.UploadFile(uriString,"PUT",fileName);