[Fs4]Open TextFiles
Aplikasi saya buat dengan ide pengamanan FileText (.txt) di dalam folder tertentu dan me-lock nya, jadi tidak sembarang orang dapat membuka folder tersebut yang didalamnya berisi FileText yang penting atau bersifat pribadi : seperti password atau sebagainya ^_^ …
Oke langsung saya bahas :
1. Buat Form Baru
Klik File -> New – Project [ctrl + shift + N]
2. Pilih Windows Form Application
3. Setting Form Seperti Gambar di atas ^_^ atau terserah agan
Tambahkan:
contextMenuStrip1
openFileDialog1
saveFileDialog1
folderBrowserDialog1
&
using System.IO;
using System.Diagnostics;
using System.Security.AccessControl;
Berikut Syntax Form1.cs
private void Openbutton_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "[Fs4]OpentextFiles";
folderName = folderBrowserDialog1.SelectedPath;
//Filter to only text files
openFileDialog1.Filter = "TextFile|*.txt";
openFileDialog1.FileName = String.Empty;
//Open file dialog and store the returned value
DialogResult result = openFileDialog1.ShowDialog();
//If Open Button was pressed
if (result == DialogResult.OK)
{
//Create a stream which points to the file
Stream fs = openFileDialog1.OpenFile();
//Create a reader using the stream
StreamReader reader = new StreamReader(fs);
//Read Contents
richTextBox1.Text = reader.ReadToEnd();
textBoxName.Text = openFileDialog1.FileName;
//this.label1.Text = folderBrowserDialog1.SelectedPath;
//Close the reader and the stream
reader.Close();
}
}
private void Savebutton_Click(object sender, EventArgs e)
{
saveFileDialog1.Title = "[Fs4]SavetextFiles";
//Specify the extensions allowed
saveFileDialog1.Filter = "Text File|.txt";
//Empty the FileName text box of the dialog
saveFileDialog1.FileName = textBoxName.Text;
//Set default extension as .txt
//saveFileDialog1.DefaultExt = textBoxName.Text;
//Open the dialog and determine which button was pressed
DialogResult result = saveFileDialog1.ShowDialog();
//If the user presses the Save button
if (result == DialogResult.OK)
{
//Create a file stream using the file name
FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create);
//Create a writer that will write to the stream
StreamWriter writer = new StreamWriter(fs);
//Write the contents of the text box to the stream
writer.Write(richTextBox1.Text);
//Close the writer and the stream
writer.Close();
}
}
Pada contextMenuStrip1
private void saveTextToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1.Title = "[Fs4]OpentextFiles";
saveFileDialog1.Filter = "Text File|.txt";
saveFileDialog1.FileName = textBoxName.Text;
DialogResult result = saveFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create);
StreamWriter writer = new StreamWriter(fs);
writer.Write(richTextBox1.Text);
writer.Close();
}
}
private void buttonBrowse_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
textBoxFile.Text = folderBrowserDialog1.SelectedPath;
}
}
private void buttonLock_Click(object sender, EventArgs e)
{
try
{
string folderPath = textBoxFile.Text;
string adminUserName = Environment.UserName;// getting your adminUserName
DirectorySecurity ds = Directory.GetAccessControl(folderPath);
FileSystemAccessRule fsa = new FileSystemAccessRule(adminUserName, FileSystemRights.FullControl, AccessControlType.Deny);
ds.AddAccessRule(fsa);
Directory.SetAccessControl(folderPath, ds);
MessageBox.Show("Locked");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void buttonUnlock_Click(object sender, EventArgs e)
{
try
{
string folderPath = textBoxFile.Text;
string adminUserName = Environment.UserName;// getting your adminUserName
DirectorySecurity ds = Directory.GetAccessControl(folderPath);
FileSystemAccessRule fsa = new FileSystemAccessRule(adminUserName, FileSystemRights.FullControl, AccessControlType.Deny);
ds.RemoveAccessRule(fsa);
Directory.SetAccessControl(folderPath, ds);
MessageBox.Show("UnLocked");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Demo Project
Terima Kasih ^_^