Archive for Agosto 28th, 2008

Log string to text file log.

CodeKeep C# Feed Agosto 28th, 2008

Description: Logs a string to a text file log.

Link: http://www.codekeep.net/snippets/39197bb5-8822-4188-8f21-a6fdda4553d6.aspx

	/// <summary>
        /// Logs string to text file.
        /// Private helper method.
        /// </summary>
        /// <param name="textToLog">The text logged to file.</param>
        private static void Log(string textToLog)
        {
            TextWriter tw = File.AppendText(@"C:\Log.txt");
            tw.WriteLine(DateTime.Now + ":\t" + textToLog);
            tw.Close();
        }

Convert a Hex string to Ascii

CodeKeep C# Feed Agosto 28th, 2008

Description: Converts a Hex encoded string to Ascii

Link: http://www.codekeep.net/snippets/e6405372-c850-44c4-8d03-0f7570ef8bbe.aspx

/// <summary>
        /// Converts a Hex string to Ascii.
        /// Private helper method.
        /// </summary>
        /// <param name="Data">A Hex encoded string</param>
        /// <returns>An Ascii encoded string</returns>
        private static string Hex_Asc(string Data)
        {
            // Create a string variable to hold each character as it's converted
            string Data1 = string.Empty;

            // Create a string variable to hold the aggregated character data.
            string sData = string.Empty;

            // While there is data left to convert
            while (Data.Length > 0)
            {
                // Take a hex pair from the Data string passed in, convert to UInt from base 16, convert
                // the int to a Char and output it as a string
                Data1 = System.Convert.ToChar(System.Convert.ToUInt32(Data.Substring(0, 2), 16)).ToString();

                // Add the character onto our already converted data
                sData = sData + Data1;

                // Remove the hex pair we just converted from the data passed in
                Data = Data.Substring(2, Data.Length - 2);

            }

            // Return the converted string
            return sData;
        }

Simple File Upload Script

CodeKeep C# Feed Agosto 28th, 2008

Description: Simple File Upload Script

Link: http://www.codekeep.net/snippets/fe5f26d3-064d-474a-ab1c-11a439ccc064.aspx

bool hasfile = frmFileUploadPhoto.HasFile;

		if (frmFileUploadPhoto.HasFile)
		{
			string extension = System.IO.Path.GetExtension(frmFileUploadPhoto.FileName).ToLower();

			string[] filetypes = { ".jpg", ".gif", ".png", ".bmp" };

			bool acceptableFile = false;
			foreach (string type in filetypes)
			{
				if (extension == type)
					acceptableFile = true;
			}
			if (acceptableFile)
			{
				FileInfo fi = new FileInfo(Path.GetFullPath(frmFileUploadPhoto.FileName));
				frmFileUploadPhoto.SaveAs(Path.GetFullPath(frmFileUploadPhoto.FileName));
				ImageProcess ip = new ImageProcess();

				ip.FileImage = System.Drawing.Image.FromFile(fi.FullName);
				System.Drawing.Bitmap img = ip.RenderImage();

				MemoryStream stream = new MemoryStream();

				img.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
				int len = (int)stream.Length;

				if (len <= 200000000)
				{

					byte[] bytes = stream.ToArray(); 
					bytes = stream.GetBuffer(); 

					aPhoto.Photo = bytes;
					aPhoto.ApplicationId = ex.ApplicationID;
					aPhoto.PhotoDate = DateTime.Now;
					ubo.AddApplicationPhotos(aPhoto);
				}
				stream.Close();
			}
			else
			{
				System.Diagnostics.Debug.Write("Not a valid file");
			}

Simple Global.asax Error Logger

CodeKeep C# Feed Agosto 28th, 2008

Description: Simple Global.asax Error Logger

Link: http://www.codekeep.net/snippets/2b488b9e-3554-4b48-b056-ae3bbdd340fb.aspx

 void Application_Error(object sender, EventArgs e)
    {
        Exception objErr = Server.GetLastError().GetBaseException();
        HttpContext.Current.Response.Clear();
        
        string strMsg = "Error Caught in Application_Error event\n" +
        "Error in: " + Request.Url.ToString() + 
        "\nError Message:" + objErr.Message.ToString() +
        "\n\nREMOTE_ADDR: " + Request.ServerVariables["REMOTE_ADDR"] +
        "\nHTTP_ACCEPT_LANGUAGE: " + Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"] +
        "\nHTTP_REFERER: " + Request.ServerVariables["HTTP_REFERER"] +
        "\nHTTP_USER_AGENT: " + Request.ServerVariables["HTTP_USER_AGENT"] +
        "\nHTTP_COOKIE: " + Request.ServerVariables["HTTP_COOKIE"];
        try
        {
            strMsg += "\n\n\nStack Trace:" + objErr.StackTrace.ToString();
        }
        catch
        {
        }
        if (!System.Diagnostics.EventLog.SourceExists("A_Application"))
        {
            System.Diagnostics.EventLog.CreateEventSource("A_Application", "Application");
        }

        System.Diagnostics.EventLog myLog = new System.Diagnostics.EventLog();
        myLog.Source = "A_Application";

        myLog.WriteEntry(strMsg, System.Diagnostics.EventLogEntryType.Error);
        Server.ClearError();
         
        Server.Transfer("~/ErrorPage.aspx");
    }