most forms use "using System.IO;"
WebForm SignOn
private void butSignOn_Click( )
string strPath; // Declare string Path variable
strPath = Server.MapPath(Request.ApplicationPath) + "\\" + txtName.Text; // Use IO to create Path string
if (Directory.Exists(strPath)) // Check if Path exists
{ Session["Path"] = strPath; // Set Session Variable named "Path"
Server.Transfer("FileManager.aspx"); } // Navigate to FileManager page
// THE FOLLOWING CODE IS IN THE HTTP PAGE
<p>New users click <a href="NewAccount.aspx">here</a> to set up an account</p> // This takes you to the NewAccount account page
WebForm FileManager
string strPath; // Class variable
public string[] strFiles; // Class array
private void Page_Load( )
strPath = Session["Path"].ToString(); // Put Session variable in class variable
if (!Page.IsPostBack) // If this is not a post-back event.
{ strFiles = Directory.GetFiles(strPath); // Get list of files in the current directory
for (int iCount = 0; iCount <= strFiles.GetUpperBound(0); iCount++) // Get the short names for the files
strFiles[iCount] = Path.GetFileName(strFiles[iCount]); } // and populate the array
lstFiles.DataBind(); // Bind lstFiles to file array
// NOTE: Make sure the DataSource for the listbox is set to "strFiles", an array
private void butNew_Click(object sender, System.EventArgs e)
{ if (!(txtNewFile.Text == "")) // If there is a file name in the text box to create a new
Response.Redirect("EditFile.aspx?file=" + txtNewFile.Text); // file, navigate to the page, passing the files name
else
litNoFile.Text = "<p>You must enter the name of a file to create.<p>"; } // Otherwise, display a message
private void butEdit_Click(object sender, System.EventArgs e)
{ if (lstFiles.SelectedItem != null) // If there is a file name, then start the editor
Response.Redirect("EditFile.aspx?file=" + lstFiles.SelectedItem.ToString()); // Navigate to new Page with File name from listbox
else
litNoneSelected.Text = "<p>You must select a file.<p>"; } // Otherwise, display a message
private void butDelete_Click(object sender, System.EventArgs e)
{ if (lstFiles.SelectedItem != null) // If there is a file name, then start the editor
try {
File.Delete(strPath + "\\" + lstFiles.SelectedItem.ToString()); // Try to delete the file in the listbox
lstFiles.Items.Remove(lstFiles.SelectedItem); } // Remove the item from the listbox
catch (System.UnauthorizedAccessException ex) {
Server.Transfer("NotAuthorized.aspx"); } // Go to not authorized page for access error
else
litNoneSelected.Text = "<p>You must select a file.<p>"; } // Otherwise, display a message.
WebForm EditFile
string strPath; // Class Path variable
string strFile; // Class FileName variable
private void Page_Load(object sender, System.EventArgs e)
{ strPath = Session["Path"].ToString(); // Get Path from Session State variable
strFile = Request.QueryString["file"]; // Get File name from QueryString variable 'file'
if (! Page.IsPostBack) // If this is not a post-back event.
{ StreamReader strmEditFile; // Create a StreamReader object
try {
strmEditFile = File.OpenText(strPath + "\\" + strFile); // Open the file
txtEditFile.Text = strmEditFile.ReadToEnd().ToString(); // Read its text.
strmEditFile.Close(); } // Close the file
catch (FileNotFoundException ex) {
File.CreateText(strPath + "\\" + strFile).Close(); // If it doesn't exist, create it
txtEditFile.Text = ""; } // Clear text box
catch (System.UnauthorizedAccessException ex) {
Server.Transfer("NotAuthorized.aspx"); }}}
private void txtEditFile_TextChanged(object sender, System.EventArgs e)
{ ViewState["Changed"] = "true"; } // Set ViewState variable to true
private void butExit_Click(object sender, System.EventArgs e)
{ if ((ViewState["Changed"] != null) && // If View state variable not null
(ViewState["Changed"].ToString() == "true")) // and equals true then save file
SaveFile(); // Call the SaveFile method
Server.Transfer("FileManager.aspx"); } // Go to the FileManager page
private void butSave_Click(object sender, System.EventArgs e)
{ SaveFile(); } // Call Save File method
void SaveFile()
{ StreamWriter strmEditWrite; // Create StreamWriter object
File.Delete(strPath + strFile); // Delete the current file in the directory path
strmEditWrite = File.CreateText(strPath + "\\" + strFile); // Create the file with the new text
strmEditWrite.Write(txtEditFile.Text); // Write the file
strmEditWrite.Close(); // Close the file
ViewState["Changed"] = "false"; } // Change View State variable to "false" for no changes
WebForm NewAccount
private void butCreate_Click(object sender, System.EventArgs e)
{ string strPath;
strPath = Server.MapPath(Request.ApplicationPath) + "\\" + txtName.Text; // Check if directory exists
if (Directory.Exists(strPath))
{ litNameExists.Text = "<p>The name " + txtName.Text + // Tell the user to choose another name
" already exists. Please choose a different one.</p>";
return; }
else
{ try
{ Directory.CreateDirectory(strPath); // Create the directory
Session["Path"] = strPath; // Set the session variable
Server.Transfer("FileManager.aspx"); } // Go to file manager
catch (System.UnauthorizedAccessException ex)
{ Server.Transfer("NotAuthorized.aspx"); } } } // Let them know their not authorized