Hello All :
Is there any idea to create a txt file by C#, I want it to save all the status of controls of the form inside it????
Thanks for support
Jump to PostExtract the generated code by VS IDE, save it to the text file.
Jump to Postbut, if you need to save some properties of the controls, I recommend to use .settings file
Here is a simple general way to write to a text file:
// FileWrite - write input from the Console into a text file
using System;
using System.IO;
namespace FileWrite
{
public class Class1
{
public static void Main(string[] args)
{
// create the filename object - the while loop allows
// us to keep trying with different filenames until
// we succeed
StreamWriter sw = null;
string sFileName = "";
while(true)
{
try
{
// enter output filename (simply hit Enter to quit)
Console.Write("Enter filename "
+ "(Enter blank filename to quit):");
sFileName = Console.ReadLine();
if (sFileName.Length == 0)
{
// no filename - this jumps beyond the while
// loop to safety
break;
}
// open file for writing; throw an exception if the
// file already exists:
// FileMode.CreateNew to create a file if it
// doesn't already exist or throw
// an exception if file exists
// FileMode.Append to create a new file or append
// to an existing file
// FileMode.Create to create a new file or
// truncate an existing file
// FileAccess possibilities are:
// FileAccess.Read,
// FileAccess.Write,
// FileAccess.ReadWrite
FileStream fs = File.Open(sFileName,
FileMode.CreateNew,
FileAccess.Write);
// generate a file stream with UTF8 characters
sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
// read one string at a time, outputting each to the
// FileStream open for writing
Console.WriteLine("Enter text; enter blank line to stop");
while(true)
{
// read next line from Console;
// quit if line file is blank
string sInput = Console.ReadLine();
if (sInput.Length == 0)
{
break;
}
// write the line just read to output file
sw.WriteLine(sInput);
}
// close the file we just created
sw.Close();
sw = null;
}
catch(IOException fe)
{
// whoops -- an error occurred somewhere during
// the processing of the file - tell the user
// what the full name of the file is:
// take the path name onto the filename
string sDir = Directory.GetCurrentDirectory();
string s = Path.Combine(sDir, sFileName);
Console.WriteLine("Error on file {0}", s);
// now output the error message in the exception
Console.WriteLine(fe.Message);
}
}
// wait for user to acknowledge the results
Console.WriteLine("Press Enter to terminate...");
Console.Read();
}
}
}
Thank you very much ,,,, but I was wonder if I have a form inside it checkbox textbox ...etc while I want to press my save button I want a directly text file created in any partition (without save dialog) saving the status of those controls ....so can I do it in C#??????
thanks for support....
Extract the generated code by VS IDE, save it to the text file.
but, if you need to save some properties of the controls, I recommend to use .settings file
Hello :
Thanks a lot , but how can I create s setting file can you explain please ???
Project->add new item->Settings file
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.