I'm wondering how do you write a text file to a specific location on the desktop such as C:\Users\tommy\desktop\transaction\ using StreamWriter?
private void CreateTransaction_Click(object sender, EventArgs e)
{
StreamWriter outputFile;
outputFile = File.CreateText (TID.ToString()+".txt");
outputFile.WriteLine("Investor :" +" " + InvestorNameLabel.Text);
outputFile.WriteLine("Initial Amount" + " " +AmountLabel.Text);
outputFile.WriteLine("Date Invested" +" " +DateLabel.Text);
outputFile.WriteLine("Period Chosen" + " "+DaysInvestedLabel.Text);
outputFile.WriteLine("Rate Chosen" + " " + RateLabel.Text);
outputFile.WriteLine("Total Interest" + " " +InterestAmountLabel.Text);
outputFile.WriteLine("Transaction Number :" + " " + TransactionIDLabel.Text);
outputFile.Close();
MessageBox.Show("Transaction file for Transaction: " + TransactionIDLabel.Text + "Was Created", "Transaction File");
}
If you know the path,
FileStream fs = new FileStream(#"C:\Users\tommy\desktop\transaction\\result.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine("Investor :" +" " + InvestorNameLabel.Text);
sw.WriteLine("Initial Amount" + " " +AmountLabel.Text);
sw.WriteLine("Date Invested" +" " +DateLabel.Text);
sw.WriteLine("Period Chosen" + " "+DaysInvestedLabel.Text);
sw.WriteLine("Rate Chosen" + " " + RateLabel.Text);
sw.WriteLine("Total Interest" + " " +InterestAmountLabel.Text);
sw.WriteLine("Transaction Number :" + " " + TransactionIDLabel.Text);
sw.Flush();
sw.Close();
else create a file inside the desktop folder and do the above
Use Environment.SpecialFolder to get the path to the desktop.
string strPath = Environment.GetFolderPath(
System.Environment.SpecialFolder.DesktopDirectory);
Related
I have to take a file from a folder and make a archive in another folder.
I use this code but get an error:
************** Exception Text **************
System.IO.IOException: The directory name is invalid.
I use this code:
private void button1_Click(object sender, EventArgs e)
{
string Info = "";
string startPath = #"D:\dosfiles\SYNOPD\SYNOPD$$.PD\" + comboBox3.SelectedItem.ToString() + #"\" + comboBox2.SelectedItem.ToString() + #"\" + station;
string zipPath = #"D:\dosfiles\SYNOPD\" + comboBox1.SelectedItem.ToString() + "_" + comboBox2.SelectedItem.ToString() + "_" + comboBox3.SelectedItem.ToString() + ".zip";
if (File.Exists(zipPath))
{
File.Delete(zipPath);
ZipFile.CreateFromDirectory(startPath, zipPath);
Info += "Вашият архив е създаден в папка D:\\dosfiles\\SYNOPD\\" + comboBox1.SelectedItem.ToString() + "_" + comboBox2.SelectedItem.ToString() + "." + comboBox3.SelectedItem.ToString() + ".zip" + Environment.NewLine;
}
else
{
ZipFile.CreateFromDirectory(startPath, zipPath);
Info += "Вашият архив е създаден в папка D:\\dosfiles\\SYNOPD\\" + comboBox1.SelectedItem.ToString() + "_" + comboBox2.SelectedItem.ToString() + "." + comboBox3.SelectedItem.ToString() + ".zip" + Environment.NewLine;
}
this.Hide();
Form1 frm = new Form1();
frm.Show();
}
I want to make my program that i made way shorter then it is right now.
I make 8 different objects and do things with them but I want to make it with something like a for loop and an index number.
Is there a way to do this, because i've looked it up but don't seem to find anything.
The objects and such are in Dutch sorry for that.
private void AankomstButton_Click(object sender, EventArgs e)
{
if (welkKind == 1)
{
if (File.Exists(#"Bestanden/" + kind1.Naam + "_" + DateTime.Now.ToString("Y") + ".txt") == false)
{
using (StreamWriter sw = new StreamWriter(#"Bestanden/" + kind2.Naam + "_" + DateTime.Now.ToString("Y") + ".txt")) ;
}
else
{
using (StreamWriter sw = File.AppendText(#"Bestanden/" + kind2.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
sw.WriteLine(DateTime.Now.ToString("d") + " Aangekomen: " + DateTime.Now.ToString("t"));
}
}
}
if (welkKind == 2)
{
if (File.Exists(#"Bestanden/" + kind2.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
using (StreamWriter sw = File.AppendText(#"Bestanden/" + kind2.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
sw.WriteLine(DateTime.Now.ToString("d") + " Aangekomen: " + DateTime.Now.ToString("t"));
}
}
else
{
using (StreamWriter sw = new StreamWriter(#"Bestanden/" + kind2.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
sw.WriteLine(DateTime.Now.ToString("d") + " Aangekomen: " + DateTime.Now.ToString("t"));
}
}
}
if (welkKind == 3)
{
if (File.Exists(#"Bestanden/" + kind3.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
using (StreamWriter sw = File.AppendText(#"Bestanden/" + kind3.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
sw.WriteLine(DateTime.Now.ToString("d") + " Aangekomen: " + DateTime.Now.ToString("t"));
}
}
else
{
using (StreamWriter sw = new StreamWriter(#"Bestanden/" + kind3.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
sw.WriteLine(DateTime.Now.ToString("d") + " Aangekomen: " + DateTime.Now.ToString("t"));
}
}
}
if (welkKind == 4)
{
if (File.Exists(#"Bestanden/" + kind4.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
using (StreamWriter sw = File.AppendText(#"Bestanden/" + kind4.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
sw.WriteLine(DateTime.Now.ToString("d") + " Aangekomen: " + DateTime.Now.ToString("t"));
}
}
else
{
using (StreamWriter sw = new StreamWriter(#"Bestanden/" + kind4.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
sw.WriteLine(DateTime.Now.ToString("d") + " Aangekomen: " + DateTime.Now.ToString("t"));
}
}
}
if (welkKind == 5)
{
if (File.Exists(#"Bestanden/" + kind5.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
using (StreamWriter sw = File.AppendText(#"Bestanden/" + kind5.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
sw.WriteLine(DateTime.Now.ToString("d") + " Aangekomen: " + DateTime.Now.ToString("t"));
}
}
else
{
using (StreamWriter sw = new StreamWriter(#"Bestanden/" + kind5.Naam + "_" + DateTime.Now.ToString("Y") + ".txt"))
{
sw.WriteLine(DateTime.Now.ToString("d") + " Aangekomen: " + DateTime.Now.ToString("t"));
}
}
}
If you keep an array of children (kind), you can access the array by index.
Kind[] children = new Kind[] { kind1, kind2 };
if (welkKind >= 0 && welkKind < children.Length)
{
Kind kind = children[welkKind];
string fileName = #"Bestanden/" + kind.Naam + "_" + DateTime.Now.ToString("Y") + ".txt";
if (File.Exists(fileName))
{
using (StreamWriter sw = new StreamWriter(fileName))
{
sw.WriteLine(DateTime.Now.ToString("d") + " Aangekomen: " + DateTime.Now.ToString("t"));
}
}
else
{
using (StreamWriter sw = File.AppendText(fileName))
{
sw.WriteLine(DateTime.Now.ToString("d") + " Aangekomen: " + DateTime.Now.ToString("t"));
}
}
}
Another option would be a list, which would have similar syntax in its usage, but you can dynamically add and remove items from the list:
List<Kind> children = new List<Kind>();
children.Add(kind1);
children.Add(kind2);
children.Add(new Kind() { Naam = "John" });
if (welkKind >= 0 && welkKind < children.Count)
{
Kind kind = children[welkKind];
}
In this code :
static void Main(string[] args)
{
var wa = System.Security.Principal.WindowsIdentity.GetCurrent();
Console.WriteLine("Type: " + wa.AuthenticationType + Environment.NewLine +
"ImpersonationLevel: " + wa.ImpersonationLevel + Environment.NewLine +
"IsAnonymous: " + wa.IsAnonymous + Environment.NewLine +
"IsAuthenticated: " + wa.IsAuthenticated + Environment.NewLine +
"IsGuest: " + wa.IsGuest + Environment.NewLine +
"IsSystem: " + wa.IsSystem + Environment.NewLine);
Console.WriteLine("Windows Account: " + wa.Name);
Console.WriteLine("Domain: " + Environment.UserDomainName);
Console.WriteLine("UserName: " + Environment.UserName);
Console.WriteLine("Interactive: " + Environment.UserInteractive);
try
{
System.IO.Directory.GetFiles(#"NETWORK SHARED DIRECTORY PATH");
Console.WriteLine("Success");
}
catch (Exception ex)
{
Console.WriteLine("Fail: " + ex.ToString());
}
Console.Read();
}
I try get the files in a shared repository in a server. If excute the program, it work. But if I use RunAs, It throw the exeption :
System.IOException "Logon failure: unknown user name or bad password."
Except the WriLines in TryCath, the output is the same. The crendential is identical.
Can you help me to resolve this problem?
Maybe I need display other informations?
Thank.
Is there a way to get the message box to close and return to the form without it emailing the data?
halfway there atm just need a help with the second if/else if statement. I'm very new to all this.
my code is below :)
//Button
private void submitButton_Click(object sender, EventArgs e)
{
string software = null;
string hardware = null;
foreach (string s in softwareNeededCheckedListBox.CheckedItems)
{
software = software + '\n' + s;
}
foreach (string s in hardwareNeededCheckedListBox.CheckedItems)
{
hardware = hardware + '\n' + s;
}
if (yesRadioButton.Checked == true)
{
yesRadioButton.Text = "Yes";
noRadioButton.Text = " ";
}
if (noRadioButton.Checked == true)
{
noRadioButton.Text = "No";
yesRadioButton.Text = " ";
}
if (MessageBox.Show("First name: " + firstNameTextBox.Text + " " + "Last name: " + lastNameTextBox.Text + "\n" + "\n" +
"Will be working in " + areaOfTheCompanyComboBox.SelectedItem + "\n" + "\n" +
"They will need the following software:" + software + "\n" + "\n" +
"They will need the following hardware:" + hardware + "\n" + "\n" +
"They will be seated: " + seatingLocationRichTextBox.Text + "\n" + "\n" +
"Any further Comments: " + notesRichTextBox.Text + "\n" + "\n" +
"Do they require a phone: " + noRadioButton.Text + yesRadioButton.Text + "\n" + "\n" +
"Date they start: " + completionDateTimePicker.Text + "\n" + "\n" +
"Are you happy with the information shown above?" + "\n" + "\n" +
MessageBoxButtons.OKCancel) == DialogResult.OK)
{
MailMessage mail = new MailMessage();
SmtpClient smtpserver = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("xxxxx#gmail.com");
mail.To.Add("xxxxx#xxxxxx.co.uk");
mail.Subject = "Test Mail";
mail.Body = "First name: " + firstNameTextBox.Text + " " + "Last name: " + lastNameTextBox.Text + "\n" + "\n" +
"Will be working in " + areaOfTheCompanyComboBox.SelectedItem + "\n" + "\n" +
"They will need the following software:" + software + "\n" + "\n" +
"They will need the following hardware:" + hardware + "\n" + "\n" +
"They will be seated: " + seatingLocationRichTextBox.Text + "\n" + "\n" +
"Any further Comments: " + notesRichTextBox.Text + "\n" + "\n" +
"Do they require a phone: " + noRadioButton.Text + yesRadioButton.Text + "\n" + "\n" +
"Date they start: " + completionDateTimePicker.Text;
smtpserver.Port = 587;
smtpserver.Credentials = new System.Net.NetworkCredential("**", "********");
smtpserver.Send(mail);
MessageBox.Show("Your Form has been sent ");
}
You actually forgot you place commas to your MessageBox.Show to have the correct arguments/parameters.
if (MessageBox.Show("Body of your message box",
"Title Of Your MessageBox",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Information) == DialogResult.OK)
{
}
That will only e-mail if the user clicks OK. You can handle the cancel button by using DialogResult.Cancel
I would personally put the MessageBox in a dialogResult variable. For instance:
var dialogResult = MessageBox.Show("FIrst name .... ");
Then you can check if the user pressed OK or Cancel
if (dialogResult == DialogResult.OK) {
// Send the e-mail
} else if (dialogResult == DialogResult.Cancel) {
// Do what you need to do / return to the form.
}
To show a messagebox with OK and Cancel you need:
MessageBox.Show(this, "Message", "Title", MessageBoxButtons.OKCancel);
I have a trouble with the following code. It should prevent overwriting records in the text file. My code is like that:
public static void WritingMethod()
{
StreamWriter Sw = new StreamWriter("fileone.txt", true);
string output = string.Format("Thank you for registration! Your Submitted information are:" + Environment.NewLine + "Name: {0}"
+ Environment.NewLine + "ID: {1}" + Environment.NewLine + "Age: {2}" + Environment.NewLine + "E-mail: {3}", Name, ID, Age, Email);
// I using the Environment.NewLine to insert new lines
Console.WriteLine(output);
Sw.WriteLine(output + Environment.NewLine);
Sw.Close();
}
It overwrites the records. I want to add records and not overwrites it.
Open your StreamWriter using File.AppendText:
using (StreamWriter Sw = File.AppendText("fileone.txt"))
{
string output = string.Format("Thank you for registration! Your Submitted information are:" + Environment.NewLine + "Name: {0}"
+ Environment.NewLine + "ID: {1}" + Environment.NewLine + "Age: {2}" + Environment.NewLine + "E-mail: {3}", Name, ID, Age, Email);
Console.WriteLine(output);
Sw.WriteLine(output + Environment.NewLine);
}