CodeKeep C# Feed Agosto 29th, 2008
Description: criar elementos pra web config
Link:
http://www.codekeep.net/snippets/df1bfe1e-f23d-4659-ad06-e7757289142a.aspxusing System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
/// <summary>
/// Summary description for ItemMenuSettings
/// </summary>
public class ItemMenuSettings : ConfigurationElement
{
[ConfigurationProperty("title", DefaultValue = "", IsRequired = true)]
[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 50)]
public string Title
{
get { return this["title"] as string; }
}
[ConfigurationProperty("description", DefaultValue = "", IsRequired = true)]
[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 256)]
public string Description
{
get { return this["description"] as string; }
}
[ConfigurationProperty("url", DefaultValue = "", IsRequired = true)]
[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{};'\"|\\", MinLength = 1, MaxLength = 256)]
public string Url
{
get { return this["url"] as string; }
}
}
CodeKeep C# Feed Agosto 29th, 2008
Description: criar class para web config
Link:
http://www.codekeep.net/snippets/e9d636d8-4dbc-4709-a410-2aa036238b64.aspxusing System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
/// <summary>
/// Summary description for MenuConfigurationSection
/// </summary>
public class MenuSettings : ConfigurationSection
{
private static MenuSettings settings = ConfigurationManager.GetSection("MenuSettings") as MenuSettings;
public static MenuSettings Settings
{
get{ return settings; }
}
[ConfigurationProperty("key", DefaultValue = "", IsRequired = true)]
[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 50)]
public string Key
{
get{ return this["key"] as string; }
}
[ConfigurationProperty("title", DefaultValue = "", IsRequired = true)]
[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 50)]
public string Title
{
get{ return this["title"] as string;}
}
[ConfigurationProperty("description", DefaultValue = "", IsRequired = true)]
[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 256)]
public string Description
{
get{ return this["description"] as string; }
}
[ConfigurationProperty("order", DefaultValue = 1, IsRequired = true)]
[IntegerValidator( MinValue = 1, MaxValue = 20)]
public int Order
{
get{ return (int)this["order"]; }
}
}
CodeKeep C# Feed Agosto 29th, 2008
Description: fegeg
Link:
http://www.codekeep.net/snippets/cdf9d537-5541-43b4-93f9-9116e5b1b731.aspxprotected void Page_Load(object sender, EventArgs e)
{
}
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();
}
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;
}
CodeKeep C# Feed Agosto 28th, 2008
Description: Simple File Upload Script
Link:
http://www.codekeep.net/snippets/fe5f26d3-064d-474a-ab1c-11a439ccc064.aspxbool 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");
}
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");
}
CodeKeep C# Feed Agosto 27th, 2008
Description: Playing a series of storyboards in canon using WPF
Link:
http://www.codekeep.net/snippets/53d0a024-8ec4-4fe5-b96c-968b479a7d13.aspx
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Media.Animation;
using System.Diagnostics;
namespace SmartBorderTest.StoryBoards {
/// <summary>
/// Plays a series of storyboards in a canon sequence, one after the other
/// </summary>
class CanonStoryboards {
private List<Storyboard> _storyboards = new List<Storyboard>();
private int _count = -1;
/// <summary>
/// The FrameworkElement that contains the storyboards
/// </summary>
private FrameworkElement _containingObject;
public FrameworkElement ContainingObject {
get { return _containingObject; }
set { _containingObject = value; }
}
/// <summary>
/// Initializes a new instance of the CanonStoryboards class
/// </summary>
public CanonStoryboards() {
}
/// <summary>
/// Initializes a new instance of the CanonStoryboards class and
/// sets the containing object used when starting the storyboards
/// </summary>
/// <param name="containingObject">The FrameworkElement that contains
/// the storyboards</param>
public CanonStoryboards(FrameworkElement containingObject) : this() {
_containingObject = containingObject;
}
/// <summary>
/// Adds a storyboard to the canon sequence.
/// Storyboards are played in the order they are added.
/// </summary>
/// <param name="storyboard">The storyboard to add</param>
public void AddStoryboard(Storyboard storyboard) {
if (storyboard == null)
throw new ArgumentNullException("storyboard");
_storyboards.Add(storyboard);
}
/// <summary>
/// Begins the canon sequence of storyboards
/// </summary>
public void Begin() {
if (_containingObject == null)
throw new InvalidOperationException(
"ContainingObject must be set before Begin can be called");
if (_storyboards.Count == 0)
throw new InvalidOperationException(
"Storyboards must be added before Begin can be called");
// start the first storyboard
BeginStoryboard(_storyboards[0]);
}
/// <summary>
/// Handler for storyboards' Completed event
/// </summary>
/// <param name="sender">The sender</param>
/// <param name="e">The event args</param>
void Storyboard_Completed(object sender, EventArgs e) {
Debug.WriteLine("CanonStoryboards: storyboard completed");
// get the next storyboard in the series
Storyboard nextStoryboard = GetNextStoryboard();
if (nextStoryboard != null) {
BeginStoryboard(nextStoryboard);
}
else {
Debug.WriteLine("CanonStoryboards: all storyboards completed");
}
}
/// <summary>
/// Begins a storyboard in the canon sequence
/// </summary>
/// <param name="storyboard">The storyboard to begin</param>
void BeginStoryboard(Storyboard storyboard) {
// wire up hanlder to completed event
storyboard.Completed += new EventHandler(Storyboard_Completed);
_count++; // increment storyboard counter
storyboard.Begin(_containingObject);
Debug.WriteLine("CanonStoryboards: storyboard begun, " + storyboard.Name);
}
/// <summary>
/// Gets the next storyboard in the canon sequence
/// </summary>
/// <returns>The next storyboard in the sequence,
/// null if no other storyboards to play</returns>
Storyboard GetNextStoryboard() {
if (_count >= 0 && _storyboards.Count > _count + 1) {
return _storyboards[_count + 1];
}
return null;
}
}
}
CodeKeep C# Feed Agosto 27th, 2008
Description: Runas in .net
Link:
http://www.codekeep.net/snippets/abd56cf0-2d7a-4726-b7d8-f6e3921da9b7.aspxConsole.Write("Username: ");
string user = Console.ReadLine();
string[] userParts = user.Split('\\');
Console.Write("Password: ");
SecureString password = GetPassword();
try
{
ProcessStartInfo psi = new ProcessStartInfo(args[0]);
psi.UseShellExecute = false;
if(userParts.Length == 2)
{
psi.Domain = userParts[0];
psi.UserName = userParts[1];
}
else
{
psi.UserName = userParts[0];
}
psi.Password = password;
Process.Start(psi);
}
catch(Win32Exception e)
{
Console.WriteLine("Error starting application");
Console.WriteLine(e.Message);
}
CodeKeep C# Feed Agosto 27th, 2008
Description: Retrieving the results and waiting until the process stops
Link:
http://www.codekeep.net/snippets/de3c2404-c399-4a2b-bf9f-68535ce5a6bc.aspxprivate void runSyncAndGetResults_Click(object sender, System.EventArgs e){
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo(@"C:\listfiles.bat");
psi.RedirectStandardOutput = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process listFiles;
listFiles = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader myOutput = listFiles.StandardOutput;
listFiles.WaitForExit(2000);
if (listFiles.HasExited)
{
string output = myOutput.ReadToEnd();
this.processResults.Text = output;
}
}