因此,我正在编写一个c#程序,它将返回在文本框中选择的文本文件string[]。
但是,当尝试这样做时,它会出现一个错误,即“静态本地函数不能包含对此或基的引用”。
下面是收集文本框字符串的代码
string[] words = { File.ReadAllText(dicuploadname.Text)这是出现错误的地方。所涉文本框的代码如下:
Private void uploaddict_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog();
MessageBox.Show("The dictionary will need to be a .txt file.");
fdlg.Title = "Select your dictionary file";
fdlg.InitialDirectory = @"c:\";
fdlg.Filter = "Text files (*.txt)|*.txt";
fdlg.FilterIndex = 2;
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
{
dicuploadname.Text = fdlg.FileName;
}
}
public static void dicuploadname_TextChanged(object sender, EventArgs e)
{
}
public string text = dicuploadname.Text;
}
}还包括浏览按钮的代码,该按钮选择文本文件并将其位置粘贴到文本框中。
不确定如何将文本文件的内容作为string[]返回?谢谢。
最新代码:
else
{
var myInt = int.Parse(min.Text);
int seconds = myInt * 60000;
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(TimerMethod);
aTimer.Interval = seconds;
aTimer.Enabled = true;
// Do something with t...
string uriString = "https://open.spotify.com/search/";
static void TimerMethod(object source, ElapsedEventArgs e)
{
string uriString = "https://open.spotify.com/search/";
WebClient webClient = new WebClient();
Random r = new Random();
string words;
words = File.ReadAllText(dicuploadname.Text);
Console.WriteLine(words[r.Next(0, words.Length)]);
string word = words[r.Next(0, words.Length)];
NameValueCollection nameValueCollection = new NameValueCollection();
nameValueCollection.Add(uriString, word);
webClient.QueryString.Add(nameValueCollection);
var spotifysearch = new ProcessStartInfo
{
FileName = "https://open.spotify.com/search/" + word,
UseShellExecute = true
};
Process.Start(spotifysearch);
}
}
}
private void HiveMind_Click_1(object sender, EventArgs e)
{
var HiveMind= new HiveMind();
HiveMind.Show();
}
private void uploaddict_Click(object sender, EventArgs e)
{
string[] words;
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Select your dictionary file";
fdlg.InitialDirectory = @"c:\";
fdlg.Filter = "Text files (*.txt)|*.txt";
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
words = File.ReadAllText(fdlg.FileName).Split(' ');
}
public string words;
private void dicuploadname_TextChanged(object sender, EventArgs e)
{
}
}
}发布于 2022-04-13 20:13:24
查看发布的代码,dicuploadname不是一个变量,而是一个事件处理程序。将其更改为此,通过读取用户指定的文本文件并在空格字符上将其拆分,从而分配变量单词。
private void uploaddict_Click(object sender, EventArgs e)
{
string[] words;
OpenFileDialog fdlg = new OpenFileDialog();
fdlg.Title = "Select your dictionary file";
fdlg.InitialDirectory = @"c:\";
fdlg.Filter = "Text files (*.txt)|*.txt";
fdlg.RestoreDirectory = true;
if (fdlg.ShowDialog() == DialogResult.OK)
words = File.ReadAllText(fdlg.FileName).Split(' ');
}在这段代码中,用户选择的文件在空格字符上被分割。如果您只想要一个字符串的代码,而不是每个单词都将单词声明更改为
string words;和变量分配给的行。
words = File.ReadAllText(fdlg.Filename);希望这能帮上忙
https://stackoverflow.com/questions/71860708
复制相似问题