我有兩個變量foreach
用戶名和密碼,我想設置爲全局變量,但我不知道如何? 我嘗試用這個代碼,但,當我嘗試使用這個變量來代替用戶名和密碼字符串我收到消息:如何在主要方法中設置全局變量c#
Use of unsigned local variable 'username'.
Use of unsigned local variable 'password'.
代碼:
public static void Main()
{
string[] lineOfContents = File.ReadAllLines(@"C:\\M\send.txt");
string username;
string password;
foreach (var line in lineOfContents)
{
string[] tokens = line.Split(',');
string user = tokens[0];
string pass = tokens[1];
username = user;
password = pass;
}
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://199.199.199.199/Plovdiv.txt");
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential(username, password);
,這是因爲你初始化變量,但沒有設置任何價值他們。你應該爲它們分配一個初始值。例如。 'string username =「」; string password =「」;' –