Simple winform editor and simple winform Editor
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Threading. Tasks;
Using System. Windows. Forms;
Using System. Data. SqlClient;
Using System. IO;
Namespace winformDemo
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
// Hide textBox2
This. textBox2.Visible = false;
// Hide the null value of the last row in the dataGridView1 table
This. Maid = false;
}
SqlConnection con = new SqlConnection ();
SqlCommand com = new SqlCommand ();
OpenFileDialog open = new OpenFileDialog ();
/// <Summary>
/// Line
/// </Summary>
String ClickRow = "";
/// <Summary>
/// Column
/// </Summary>
String ClickCells = "";
/// <Summary>
/// String of the rows and columns
/// </Summary>
String SqlLanding = "server =.; uid = sa; pwd = 123456789; database = myfirstDemo ";
Private void maid (object sender, maid e)
{
// Obtain the row and column being clicked.
ClickRow = this. Maid [e. RowIndex]. Cells [0]. Value. ToString ();
ClickCells = this. Maid [e. RowIndex]. Cells [1]. Value. ToString ();
}
Private void Form1_Load (object sender, EventArgs e)
{
SelectInfo ();
}
Public void SelectInfo ()
{
// View database data through a disconnected Link
Con. ConnectionString = SqlLanding;
Com. CommandText = "select Name as file Name, TxtLuJing as file path from TxtBianJiQi ";
Com. Connection = con;
DataSet ds = new DataSet ();
SqlDataAdapter sda = new SqlDataAdapter (com );
Sda. Fill (ds );
This. Maid = ds. Tables [0];
}
Private void open ToolStripMenuItem_Click (object sender, EventArgs e)
{
String Filepath = ClickCells + ClickRow;
This. textBox2.Visible = true;
Try
{
// Read-only stream;
FileStream fss = new FileStream (Filepath, FileMode. OpenOrCreate, FileAccess. Read );
StreamReader sww = new StreamReader (fss, Encoding. Default );
TextBox2.Text = sww. ReadToEnd ();
Sww. Close ();
Fss. Close ();
}
Catch (Exception ex)
{
// A sentence is prompted if no path is selected;
MessageBox. Show ("viewing path error:" + ex. Message );
}
}
Private void save ToolStripMenuItem_Click (object sender, EventArgs e)
{
String Filepath = ClickCells + ClickRow;
Try
{
// Write only the stream;
FileStream fss = new FileStream (Filepath, FileMode. Create, FileAccess. Write );
StreamWriter sww = new StreamWriter (fss, Encoding. Default );
Sww. Write (textBox2.Text );
Sww. Close ();
Fss. Close ();
MessageBox. Show ("saved successfully! ");
}
Catch (Exception ex)
{
// A sentence is prompted if no path is selected;
MessageBox. Show ("Saving path error:" + ex. Message );
}
This. textBox2.Visible = false;
}
Private void create ToolStripMenuItem_Click (object sender, EventArgs e)
{
This. textBox2.Text = "";
String localFilePath = "";
String fileNameExt = "";
String flie = "";
SaveFileDialog saveFileDialog = new SaveFileDialog ();
// Open the default file directory
SaveFileDialog. InitialDirectory = "D :\\\ Text \\";
// File suffix
SaveFileDialog. Filter = "text file (*. txt) | *. txt | all files (*. *) | *.*";
SaveFileDialog. FilterIndex = 2;
String LuJing = saveFileDialog. InitialDirectory;
If (saveFileDialog. ShowDialog () = DialogResult. OK)
{
Flie = saveFileDialog. FileName;
// File directory name
LocalFilePath = saveFileDialog. FileName. ToString ();
// Specifies the name of the captured object.
FileNameExt = localFilePath. Substring (localFilePath. LastIndexOf ("\") + 1 );
}
String SQL = "select name from TxtBianJiQi ";
SqlCommand co = new SqlCommand (SQL, con );
SqlDataAdapter da = new SqlDataAdapter (co );
DataSet dss = new DataSet ();
Da. Fill (dss );
// Cyclically determine the name of the input table
For (int I = 0; I <dss. Tables [0]. Rows. Count; I ++)
{
// Specify a variable to obtain the name.
String ss = dss. Tables [0]. Rows [I] [0]. ToString ();
// Check whether the value entered in the dialog box is the same as the name.
If (fileNameExt = ss)
{
MessageBox. Show ("file changed! ");
Return;
}
}
Try
{
// Write-only stream
FileStream fs = new FileStream (flie, FileMode. Create, FileAccess. Write );
StreamWriter sw = new StreamWriter (fs, Encoding. Default); // Save the dialog box.
Sw. Write (textBox2.Text );
Sw. Flush ();
Fs. Close ();
Con. ConnectionString = SqlLanding;
// SQL statement for adding the file name and path name to the database
Com. CommandText = String. Format ("insert into TxtBianJiQi (Name, TxtLuJing) values ('{0}', '{1}')", fileNameExt, LuJing );
Com. Connection = con;
Con. Open ();
Int insertInto = Convert. ToInt32 (com. ExecuteScalar ());
If (insertInto> 0)
{
MessageBox. Show ("operation failed! Please try again. ");
}
Else
{
MessageBox. Show ("added successfully! ");
This. textBox2.Visible = false;
}
}
Catch (Exception ex)
{
MessageBox. Show ("failed to add log:" + ex. Message );
}
Con. Close ();
SelectInfo ();
}
Private void Delete ToolStripMenuItem_Click (object sender, EventArgs e)
{
Con. ConnectionString = SqlLanding;
// Delete the name of the file being clicked from the database
Com. CommandText = String. Format ("delete from TxtBianJiQi where Name = '{0}'", ClickRow );
Com. Connection = con;
Con. Open ();
DialogResult dr = MessageBox. Show ("are you sure you want to delete it? "," Prompt ", MessageBoxButtons. OKCancel, MessageBoxIcon. Information );
If (dr = DialogResult. OK)
{
Int insertInto = Convert. ToInt32 (com. ExecuteScalar ());
If (insertInto> 0)
{
MessageBox. Show ("Operation Error !! ");
}
Else
{
// File. Delete (ClickCells + ClickRow); Delete files in Windows. The path of the File to be deleted is in brackets.
File. Delete (ClickCells + ClickRow );
MessageBox. Show ("deleted successfully! ");
}
}
Con. Close ();
SelectInfo ();
}
Private void exit ToolStripMenuItem_Click (object sender, EventArgs e)
{
This. Close ();
}
}
}
Suffix. Click the text name when you open or delete it. Please forgive me for not writing enough!