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);
}
     
}

Comments

Popular posts from this blog

Umbraco - Reset the Admin Password via SQL Server

Create a .NET Contact Form that Gets the Last Url Visited in C# Can also be Used in Umbraco

Umbraco Macro for IFrame when Embedding Video, Vimeo Universal Player, or other types of IFrame on to a Page XSLT version and Razor Version