System.Io is not working properly - c#

I want to copy some text on textbox2 to a txt file.I want to create a kk.txt file after clicking the button and need to store textbox2 text to that kk file.
Here is the code i tried but it only create kk.txt file and not storing textbox2 data.
private void button6_Click(object sender, EventArgs e)
{
//textBox4.Text +=Clipboard.GetText()+Environment.NewLine;
Clipboard.SetText(textBox2.Text);
System.IO.File.Create(#"C:/Ebaycodes/kk.txt");
string path = #"C:/Ebaycodes/kk.txt";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.Write(textBox2.Text);
sw.Dispose();
}
}
}
could somebody help me to fix this error.

The problem is your if statement, it only runs if the file doesn't exist. You created it, so it does exist, and the if doesn't run. You can change your entire routine to this:
private void button6_Click(object sender, EventArgs e)
{
Clipboard.SetText(textBox2.Text);
File.WriteAllText(#"c:\Ebaycodes\kk.txt", textbox2.Text);
}

Related

Listview save to a txt file

I need that all data which I can see now in Find_ list View are online added to C:\Users\Public\WattLOG.txt file
Then I Will Access to this file with another program and read all events.
When there comes new line, I need to store it immediately in file.
private void Find_listView_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Search_listView_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}

Checking if certain file\files exist

I want to make a windows form using C# to check if a file exist.
I've tried this one:
private void test_Click(object sender, EventArgs e)
{
var app1 = (#"C:\Users\frangon\Desktop\Spectrum-Check.EXE");
test.Text = File.Exists(app1).ToString();
}
If possible, I don't want to click it. I just want it to show as "True" if the file exist, or "False" if the file doesn't exist.
You can add an event to trigger when the form loads:
And then in your code behind:
private void Form1_Load(object sender, EventArgs e)
{
string app1 = #"C:\Users\frangon\Desktop\Spectrum-Check.EXE";
test.Text = File.Exists(app1).ToString();
}

C# assigning textbox contents to specific elements of an array causes some elements to be null

Hey guys so I have a userControl file which prompts to user to enter their information in which creates a text file with the information to a directory. I created multiple textboxes which have the same code. The array "info" was declared like this
private string[] oof = new string[19];
All of the textboxes are coded like this below.
private void TextBox0_TextChanged(object sender, EventArgs e) // First Name
{
info[0] = textBox0.Text;
}
private void TextBox1_TextChanged(object sender, EventArgs e) //Last Name
{
info[1] = textBox1.Text;
}
.....
The method I used to create the text file is
private void ButtonCreate_Click(object sender, EventArgs e) //Create new Profile
{
string path = #"C:\Users\J\Desktop\C#\ + profileName + ".txt";
using (StreamWriter sw = File.CreateText(path))
{
for (int i = 0; i < 13; i++)
{
sw.WriteLine(list[i]);
sw.Flush();
}
sw.Close();
sw.Dispose();
}
}
This code correctly creates the text file with the desired name to the right directory but the content of the text file contains only part of the information that the user provided when I test run it. When I debug the program, I noticed that some of the elements that were being set to the content of different textboxes were 'null' even though it was filled out by the user. It creates the text file but the file only contains part of the user's information not all of it like
first name
(last name is suppose to be here but its blank)
address
phone number
(state is suppose to be here but its blank)
city
....

Troubleshooting FileDialog trying to update textBox1

I'm looking for an "aha!" moment troubleshooting why the following works in one project, but in building a newer, more efficient version the same structure does not update the text in textBox1 with the result of openFileDialog1.
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
textBox1.Text = openFileDialog1.FileName;
}
I copied and pasted my code from Project 1 to Project 2, so I'm just not sure where the breakdown could be. The rest of the project does execute properly, so openFileDialog1 is completing successfully and allowing me to click button2 to encode a valid RSS xml file from the input given in openFileDialog1. The only thing not working is textBox1.Text displaying the FileName chosen in openFileDialog1. Any ideas as to where the breakdown is are appreciated!

Filepath and textboxes

Ok I am going to try this again. I got more information now. I understand I can not use open and save dialogs and there is no database. So I am still kinda lost cause I was shown how to do it with open and save dialogs before. I am going to put what I am suppose to do and then so far the code I have. The code I have I have to build off and add too. I will also show what I am suppose to add to it. I am just trying to find the best way to understand this cause right now I am not. I am still new and I know the last couple days people have been trying to help me understand and then I was told it wasnt with the open and save dialog. Here is what I am suppose to do.
•Add a textbox named txtFilePath <--- already have that
•Add a button next to the above textbox that says “Load” (name it appropriately)<-already have that
•Add a button that says “Save” (name it appropriately) <-- already have this
•When thebutton “Load” is clicked, read the file specified in the textbox
(txtFilePath: Absolute path not relative) and add the objects found
within to the listbox<--- Not understanding
•When the user clicks the “Save” button, write the selected record to
the file specified in txtFilePath (absolute path not relative) without
truncating the values currently inside<-- not understanding
Here is the one part of code I have:`
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void addButton_Click(object sender, EventArgs e)
{
EditDialog newEmployeeDialog = new EditDialog();
if (newEmployeeDialog.ShowDialog() == DialogResult.OK)
{
employeeList.Items.Add(newEmployeeDialog.StaffMember);
}
}
private void deleteButton_Click(object sender, EventArgs e)
{
if (employeeList.SelectedIndex == -1)
return;
if (MessageBox.Show("Really delete this employee",
"Delete", MessageBoxButtons.YesNo,
MessageBoxIcon.Question)
== DialogResult.Yes)
{
employeeList.Items.Remove(
employeeList.SelectedItem);
}
}
private void editButton_Click(object sender, EventArgs e)
{
if (employeeList.SelectedIndex == -1)
return;
int employeeNum = employeeList.SelectedIndex;
EditDialog newEmployeeDialog = new EditDialog();
newEmployeeDialog.StaffMember =
(Employee)employeeList.SelectedItem;
if (newEmployeeDialog.ShowDialog() == DialogResult.OK)
{
employeeList.Items.RemoveAt(employeeNum);
employeeList.Items.Insert(employeeNum, newEmployeeDialog.StaffMember);
employeeList.SelectedIndex = employeeNum;
}
}
private void employeeList_SelectedIndexChanged(object sender, EventArgs e)
{
if (employeeList.SelectedIndex != -1)
{
Employee currentEmployee = (Employee)employeeList.SelectedItem;
firstName.Text = currentEmployee.FirstName;
lastName.Text = currentEmployee.LastName;
jobTitle.Text = currentEmployee.JobTitle;
}
else
{
firstName.Text = lastName.Text = jobTitle.Text = "";
}
}
`
Now I know you can not see the button click but I do have them mark. I know when you use open and save how it works. How I can go about this? I would use stream writer right.I understand that the user will type the path into the textbox and when the user hits load, it will load the file that they are specified. Now I am just trying to understand a code to be able to word this right.
would it be something like this:
String filePath = this.txtFilePath.Text;
since I need to name the textbox txtFilePath. I know some of you might say this is simple but when you are first learning it don't seem that simple. I have been trying something to help me understand since I do my college from home. Thank you for reading hoping to hear from you guys.
Update: Would it be something like this
Reading a file
private void Load_Click(object sender, EventArgs e)
{
StreamReader myReader = new StreamReader(C:\\")
txtFilePath.Text = my reader.read to end();
myReader.Close();
}
then there is writing a file
{
StreamWriter myWriter = new StreamWriter("C:\\test.txt", true);
myWriter.Write("Some text");
myWriter.WriteLine("Write a line");
myWriter.WriteLine("Line 2");
myWriter.Close();
}
If this is correct then I have to get it where if the file is not there for the notepad to pop up so they can add it then they can save it without deleting anything out the file or files.
Assuming the file contains a list of employee names, you should be able to load them into your listbox using something like this:
var filepath = txtFilePath.Text;
if (File.Exists(filepath))
{
var lines = File.ReadAllLines(filepath);
foreach (var line in lines)
employeeList.Items.Add(line);
}
Then assuming you want to add a new employee name to the file that the user just entered into the listbox:
var filepath = txtFilePath.Text;
if (File.Exists(filepath))
using (var sw = File.AppendText(filepath))
sw.WriteLine((string)employeeList.Text);
else
using (var sw = File.CreateText(filepath))
sw.WriteLine((string)employeeList.Text);
This hasn't been tested, but it should work nearly as-is...

Categories

Resources