Umbraco Razor Writing to a Text File by getting a Querystring and Reading From a Text File
Please see this great post:
http://www.asp.net/web-pages/tutorials/files,-images,-and-media/working-with-files
Reading From:
@using System.IO;
@{
var result = "";
Array userData = null;
char[] delimiterChar = {','};
var dataFile = Server.MapPath("~/App_Data/MyTextFile.txt");
if (File.Exists(dataFile)) {
userData = File.ReadAllLines(dataFile);
if (userData == null) {
result = "failed";
@Html.Raw("The file is empty");
}
}
else {
result = "failed";
@Html.Raw("The file does not exist. MyTextFile.txt");
}
}
@{
if (result == "") {
foreach (string dataLine in userData) {
foreach (string dataItem in dataLine.Split(delimiterChar)) {
@dataItem
}
}
}
}
Writing To from a Querystring:
@using System.IO;
@{
var qInput = "";
if (Request[" qInput"] != null)
{
qInput = Request[" qInput"];
}
else
{
qInput = "";
}
}
@{
if qInput != "")
{
var dataFile = Server.MapPath("~/App_Data/MyTextFile.txt");
// File.AppendAllText (@dataFile, "boing"); // appends
File.WriteAllText (@dataFile, qInput);
}
}
http://www.asp.net/web-pages/tutorials/files,-images,-and-media/working-with-files
Reading From:
@using System.IO;
@{
var result = "";
Array userData = null;
char[] delimiterChar = {','};
var dataFile = Server.MapPath("~/App_Data/MyTextFile.txt");
if (File.Exists(dataFile)) {
userData = File.ReadAllLines(dataFile);
if (userData == null) {
result = "failed";
@Html.Raw("The file is empty");
}
}
else {
result = "failed";
@Html.Raw("The file does not exist. MyTextFile.txt");
}
}
@{
if (result == "") {
foreach (string dataLine in userData) {
foreach (string dataItem in dataLine.Split(delimiterChar)) {
@dataItem
}
}
}
}
Writing To from a Querystring:
@using System.IO;
@{
var qInput = "";
if (Request[" qInput"] != null)
{
qInput = Request[" qInput"];
}
else
{
qInput = "";
}
}
@{
if qInput != "")
{
var dataFile = Server.MapPath("~/App_Data/MyTextFile.txt");
// File.AppendAllText (@dataFile, "boing"); // appends
File.WriteAllText (@dataFile, qInput);
}
}
Comments
Post a Comment