我无法将文本框的内容保存到文本文件中。
我正在收集有关多个存储库的信息,并将结果打印到文本框中,并允许用户单击按钮来保存信息。
以下是按钮背后的代码:
//create a folder on the user's desktop
string saveInfoFolder = "C:/Users/" + Environment.UserName + "/Desktop/RepositoryResults";
DirectoryInfo newDirectory = new DirectoryInfo(saveInfoFolder);
if (!newDirectory.Exists)
{
newDirectory.Create();
}
//loop through each pathway and save to the text file (create it if necessary)
string saveRepositoryResults = saveInfoFolder + "/RepositoryResults.txt";
FileInfo RepositoryResults = new FileInfo(saveRepositoryResults);
if (!RepositoryResults.Exists)
{
//update the text file with the details
string createText = lstFileContents.Text;
File.WriteAllText(saveRepositoryResults, createText);
}
else
{
string createText = lstFileContents.Text;
File.WriteAllText(saveRepositoryResults, createText);
}
这应该有用,对吗?我保存到文本文件的所有内容都是存储库路径,而不是结果。
有人可以帮我吗?
谢谢! :)
答案 0 :(得分:0)
C#
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines.txt", true))
{
file.WriteLine(yourtxtbox.text);
}
答案 1 :(得分:0)
string saveInfoFolder = string.Format(@"C:/Users/{0}/Desktop/RepositoryResults", Environment.UserName);
...
string saveRepositoryResults = saveInfoFolder + "/RepositoryResults.txt";
...
string createText = lstFileContents.Text;
System.IO.File.WriteAllText(saveRepositoryResults, createText);
答案 2 :(得分:0)
您的代码在文件中编写文本看起来很好。但是当您尝试在文件中写入其内容时,Textbox.Text中的值是多少。你也可以改变这个if语句,因为else也是一样的。
if (!RepositoryResults.Exists)
{
//update the text file with the details
string createText = lstFileContents.Text;
File.WriteAllText(saveRepositoryResults, createText);
}
else
{
string createText = lstFileContents.Text;
File.WriteAllText(saveRepositoryResults, createText);
}