Archive for the '.NET' Category

Fermer ma fenêtre

CodeKeep C# Feed Novembre 17th, 2008

Description: Fermer ma fenêtre

Link: http://www.codekeep.net/snippets/bf68e3cf-6243-452b-bedd-c3a1b7a36e52.aspx

this.Close();
Application.Exit();

Modificare app.config

CodeKeep C# Feed Novembre 17th, 2008

Description: Modificare app.config

Link: http://www.codekeep.net/snippets/a05eb0b7-af31-4351-82b5-35e286ac64d4.aspx

            string configFileName = Application.StartupPath + @"\app.config";
            XmlDocument configFile = new XmlDocument();
            configFile.Load(configFileName);
            XmlNode configurations = configFile.SelectSingleNode("descendant::configuration");
            XmlNode appConfig = configFile.SelectSingleNode("descendant::appSettings");
            if (appConfig == null)
            {
                appConfig = configFile.CreateElement("appSettings");
                configurations.AppendChild(appConfig);
            }
            // il nodo deve avere la forma: <add key= "Nome del DB"
            // value="Stringa di connessione" />
            XmlNode node = configFile.CreateElement("add");
            XmlAttribute key = configFile.CreateAttribute("key");
            key.Value = "DBUTENTI";
            // aggiungo l'attributo al nodo
            node.Attributes.SetNamedItem(key);
            XmlAttribute val = configFile.CreateAttribute("value");
            val.Value = "Data Source=SRVSQLCAAF;Initial Catalog=ANAUTENTI;Integrated Security=True";
            node.Attributes.SetNamedItem(val);
            // aggiungo il nodo all'elemento superiore
            appConfig.AppendChild(node);
            node = configFile.CreateElement("add");
            key = configFile.CreateAttribute("key");
            key.Value = "DBCOMUNI";
            node.Attributes.SetNamedItem(key);
            val = configFile.CreateAttribute("value");
            val.Value = "Data Source=SRVSQLCAAF;Initial Catalog=COMUNI;Integrated Security=True";
            node.Attributes.SetNamedItem(val);
            appConfig.AppendChild(node);
            configFile.Save(configFileName);

ConfigurationElementCollectionBase

CodeKeep C# Feed Novembre 15th, 2008

Description: Generic Configuration Element Collection Base

Link: http://www.codekeep.net/snippets/6ee661a2-5269-442f-9ba2-05cc17454680.aspx

using System;
using System.Configuration;

namespace HelpersDotNet.Configuration
{
    public abstract class ConfigurationElementCollectionBase<T> :
        ConfigurationElementCollection where T : ConfigurationElement
    {
        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.AddRemoveClearMap; }
        }

        public T this[int index]
        {
            get { return (T)base.BaseGet(index); }
            set
            {
                if (base.BaseGet(index) != null)
                    base.BaseRemoveAt(index);

                base.BaseAdd(index, value);
            }
        }

        new public T this[string key]
        {
            get { return (T)base.BaseGet(key); }
        }

        public void Add(T element)
        {
            base.BaseAdd(element);
        }

        public void Clear()
        {
            base.BaseClear();
        }

        public bool Contains(string key)
        {
            return base.BaseGet(key) != null;
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return Activator.CreateInstance<T>();
        }

        public void Remove(string key)
        {
            base.BaseRemove(key);
        }

        public void RemoveAt(int index)
        {
            base.BaseRemoveAt(index);
        }
    }
}

Declaring Events

CodeKeep C# Feed Novembre 13th, 2008

Description: Example of declaring events in C#

Link: http://www.codekeep.net/snippets/87479a23-62b7-4476-bddf-711550f1a61f.aspx

public delegate void StatusUpdateHandler(string Message, int PercentComplete);
public event StatusUpdateHandler StatusUpdate;
public void OnStatusUpdate(string Message, int PercentComplete)
{
    if (StatusUpdate != null)
        StatusUpdate(Message, PercentComplete);
}

Sharepoint: Changing a MOSS Publishing Welcome Page Programatically

CodeKeep C# Feed Novembre 13th, 2008

Description: Changing a MOSS Publishing Welcome Page Programatically

Link: http://www.codekeep.net/snippets/798280ad-0f5b-4862-8aa5-6f8b33099b40.aspx

static public void UpdateWelcomePage(SPWeb web, string p)

        {

            if (PublishingWeb.IsPublishingWeb(web))

            {

                PublishingWeb pw = PublishingWeb.GetPublishingWeb(web);

                PublishingSite site = new PublishingSite(web.Site);

                //Set the default page 

                SPFile welcomefile = web.GetFile(p);

                pw.DefaultPage = welcomefile;

                pw.Update();

            }

        } 

Sharepoint: Create MySite Programmatically

CodeKeep C# Feed Novembre 13th, 2008

Description: How to create mysite programmatically

Link: http://www.codekeep.net/snippets/0609b2f5-bff3-4b8c-8b1a-02e6a840f680.aspx

using Microsoft.Office.Server;
using Microsoft.Office.Server.Administration;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.SharePoint;
using System.Web;

namespace UserProfileCreate
{
class Program
{
static void Main(string[] args)
{

using (SPSite site = new SPSite("http://servername"))
{
ServerContext context = ServerContext.GetContext(site);
UserProfileManager profileManager = new UserProfileManager(context);

string accountName = "domainname\\username";
UserProfile userProfile;
if (profileManager.UserExists(accountName))
{
userProfile = profileManager.GetUserProfile(accountName);

userProfile.CreatePersonalSite(); 

}
}
}
}
} 

Sharepoint: change current site collection’s master page

CodeKeep C# Feed Novembre 13th, 2008

Description: Change master page programmatically

Link: http://www.codekeep.net/snippets/6ad7047e-c32b-40bc-b6c6-48b154efec73.aspx

var newMaster = properties.Feature.Properties["MasterName"].Value;

			    if (!string.IsNullOrEmpty(newMaster))
				{
					using (var curSite = (SPSite)properties.Feature.Parent)
					{
                        using (var curWeb = curSite.OpenWeb(""))
                        {
                            //got the current site and root web in site, now set the master Url
                            //to our master page that should have been uploaded as part
                            //of our feature
                            if (curWeb.MasterUrl.Contains("default.master"))
                            {
                                curWeb.MasterUrl = curWeb.MasterUrl.Replace("default.master", newMaster);
                                curWeb.Update();
                                UpdateLog("MasterUrl property updated for web " + curWeb.Title,
                                    EventLogEntryType.Information);
                            }

                        }
					}
				}

ma hoa

CodeKeep C# Feed Novembre 13th, 2008

Description: cac kieu ma hoa.

Link: http://www.codekeep.net/snippets/69359a27-289a-4443-921f-de6fe6b8075c.aspx

using System;
using System.IO;
using System.Text;

public class CharacterEncodingExample
{

    public static void Main()
    {

        // T?o file gi? các k?t qu?.
        using (StreamWriter output = new StreamWriter("output.txt"))
        {

            // T?o và ghi ra file m?t chu?i ch?a ký hi?u c?a s? PI.
            string srcString = "Area = \u03A0r^2";
            output.WriteLine("Source Text : " + srcString);

            // Ghi các byte du?c mã hóa theo UTF-16
            // c?a chu?i ngu?n ra file.
            byte[] utf16String = Encoding.Unicode.GetBytes(srcString);
            output.WriteLine("UTF-16 Bytes: {0}",
            BitConverter.ToString(utf16String));

            // Chuy?n chu?i ngu?n du?c mã hóa theo UTF-16
            // thành UTF-8 và ASCII
            byte[] utf8String = Encoding.UTF8.GetBytes(srcString);
            byte[] asciiString = Encoding.ASCII.GetBytes(srcString);

            // Ghi m?ng các byte du?c mã hóa theo UTF-8 và ASCII ra file.
            output.WriteLine("UTF-8 Bytes: {0}",
            BitConverter.ToString(utf8String));
            output.WriteLine("ASCII Bytes: {0}",
            BitConverter.ToString(asciiString));

            // Chuy?n các byte du?c mã hóa theo UTF-8 và ASCII
            // thành chu?i du?c mã hóa theo UTF-16 và ghi ra file.
            output.WriteLine("UTF-8 Text : {0}",
            Encoding.UTF8.GetString(utf8String));
            output.WriteLine("ASCII Text : {0}",
            Encoding.ASCII.GetString(asciiString));

            // Ghi d? li?u xu?ng file và dóng file.
            output.Flush();
            output.Close();
        }
    }
}

Teste2

CodeKeep C# Feed Novembre 12th, 2008

Description: Teste

Link: http://www.codekeep.net/snippets/979e918f-1805-4ff6-9ec7-b8f0f297e141.aspx

rotected override void PrepareInsert()
        {
            if (GetParameterValue("HANDLE") != null)
            {
                ordemsaque.Hliquidacao = new LiquidacaoBO().SelecionarPorId((int)GetParameterValue("HANDLE"));
                bsOrdemSaque.DataSource = ordemsaque;
                bsOrdemSaque.ResetCurrentItem();
            }
        }

Union in C#

CodeKeep C# Feed Novembre 11th, 2008

Description: Union in C#

Link: http://www.codekeep.net/snippets/9ab1f74a-cf6e-4eaf-899e-f88c18d59038.aspx

    [StructLayoutAttribute(LayoutKind.Explicit)]
    struct SignedUnion
    {

        [FieldOffsetAttribute(0)]
        public sbyte Num1;
        [FieldOffsetAttribute(0)]
        public short Num2;
        [FieldOffsetAttribute(0)]
        public int Num3;
        [FieldOffsetAttribute(0)]
        public long Num4;
        [FieldOffsetAttribute(0)]
        public float Num5;
        [FieldOffsetAttribute(0)]
        public double Num6;
    }

Next »