今天我第一次尝试使用C#。我想将文件夹上传到FTP服务器并使用此FTP CLass:http://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class
现在,当我使用方法时,它说“名称ftpClient在该上下文中不存在”。我将复制我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DaloCloud
{
public partial class Form1 : Form
{
static int x = 200; // For Help-Screen
static int y = 200; // For Help-Screen
string username; // FTP-Username Value stored in there
string password; // FTP-Password Value stored in there
string dirPath; // C:\DaloUpload
string uploadPath; // ftp: //daloserver/users/username/files
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void helpButton_Click(object sender, EventArgs e)
{
// Create a new instance of Form2 and set its Visible property to true.
Form2 form2 = new Form2();
form2.Visible = true;
}
private void usernameTextbox_TextChanged(object sender, EventArgs e)
{
username = usernameTextbox.Text;
}
private void passwordTextbox_TextChanged(object sender, EventArgs e)
{
password = passwordTextbox.Text;
}
private void connectButton_Click(object sender, EventArgs e)
{
string[] files = Directory.GetFiles(dirPath, "*.*");
string[] subDirs = Directory.GetDirectories(dirPath);
foreach (string file in files)
{
ftpClient.upload(uploadPath + "/" + Path.GetFileName(file), file);
}
foreach (string subDir in subDirs)
{
ftpClient.createDirectory(uploadPath + "/" + Path.GetFileName(subDir));
recursiveDirectory(subDir, uploadPath + "/" + Path.GetFileName(subDir));
}
}
}
}
答案 0 :(得分:0)
在C#中,您必须先声明变量才能使用它们。您尚未声明任何名为ftpClient
的变量。
此错误非常简明地告诉您,ftpClient
在您使用它时并不存在。你必须声明它才能使它存在。
可能看起来像这样:
FTPClient ftpClient = new FTPClient();