无法访问外部类型的非静态成员

时间:2014-10-25 18:52:10

标签: c# asp.net ftp

错误在TextBox1.Text中,而Label2.Text是"外部类型"的非静态成员。

除了TextBox1.Text和Label2.Text之外,其他所有工作都处于良好的工作状态。

程序应该使用预先存在的ftp文件(服务器端)并将其复制到TextBox1.Text(客户端)中指定的文件。

错误代码:

  

无法访问外部类型的非静态成员' FTPDownload._Default'   通过嵌套类型' FTPDownload._Default._Default' C:\用户\用户\路径\ Default.aspx.cs

Default.aspx的

<center><table><tr><td align="center"><br /><br />
    <asp:TextBox ID="TextBox1" runat="server">C:/</asp:TextBox></td></tr><tr><td align="center"><br />
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /></td></tr><tr><td align="center"><br /><br />
    <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
</td></tr></table></center>

Default.aspx.cs

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
public partial class _Default : System.Web.UI.Page
{
        private string host = "ftp://mysite.com";
        private string user = "ftp72390003-0";
        private string pass = "pass";
        private FtpWebRequest ftpRequest = null;
        private FtpWebResponse ftpResponse = null;
        private Stream ftpStream = null;
        private int bufferSize = 2048;
    protected void Button1_Click(object sender, EventArgs e)
    {
        download("GraphingPlanets.exe", TextBox1.Text + "GraphingPlanets.exe");
    }
    public void download(string remoteFile, string localFile)
    {
        try
        {
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(host + "/" + remoteFile));
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            ftpStream = ftpResponse.GetResponseStream();
            FileStream localFileStream = new FileStream(localFile, FileMode.Create);
            byte[] byteBuffer = new byte[bufferSize];
            int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
            try
            {
                while (bytesRead > 0)
                {
                    localFileStream.Write(byteBuffer, 0, bytesRead);
                    bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
                }
            }
            catch (Exception ex) { Label2.Text = "Write: " + ex.ToString; }
            localFileStream.Close();
            ftpStream.Close();
            ftpResponse.Close();
            ftpRequest = null;
        }
        catch (Exception ex) { Label2.Text = "Total: " + ex.ToString; }
    }

1 个答案:

答案 0 :(得分:2)

C#与Java不同。内部类不带有用于创建它的外部实例的隐式引用。您必须将引用显式传递给内部类实例,以便它可以使用它。

或者,如果您不是要创建内部“_Default”类,而只是将代码添加到外部“_Default”类,则应修复代码,使其不具有嵌套类。< / p>