2015年7月19日 星期日

[C#]取得類別成員的屬性

參考
How do I get the custom attribute value of a field? [duplicate]

以取得字串最大長度為例子,取得成員的StringLength屬性

程式碼如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;

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

            foreach (var prop in typeof(RetriveClassAttribute).GetProperties())
            {
                var attrs = (StringLengthAttribute[])prop.GetCustomAttributes
                    (typeof(StringLengthAttribute), false);
                foreach (var attr in attrs)
                {
                    Console.WriteLine("{0}: {1}", prop.Name, attr.MaximumLength);
                }
            }

            Console.ReadLine();
        }

    }
    class RetriveClassAttribute
    {
        [StringLength(30)]
        public string FirstMember { get; set; }
    }
}




2015年7月15日 星期三

[C#]WCF Service 傳遞XML Data

參考
逐步解說:建立和存取 WCF 服務

WCF服務程式庫與WCF服務應用程式的不同

WCF服務應用程式原始碼

ISendXmlWcfService.svc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Xml.Linq;

namespace SendXmlWcfService
{
    public class SendXmlWcfService : ISendXmlWcfService
    {
        public string GetData(string value)
        {
            XDocument xml = XDocument.Parse(value);

            return string.Format("To {0}\r\n    {1}\r\n  {2} {3}", xml.Root.Element("to").Value, xml.Root.Element("body").Value,xml.Root.Element("heading").Value,xml.Root.Element("from").Value);
        }
    }
}

ISendXmlWcfService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace SendXmlWcfService
{
    [ServiceContract]
    public interface ISendXmlWcfService
    {
        [OperationContract]
        string GetData(string value);
    }
}

測試WCF服務應用程式-主控台應用程式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder builder = new StringBuilder("<note>" +
                                                      "<to>Tove</to>" +
                                                      "<from>Jani</from>" +
                                                      "<heading>Reminder</heading>" +
                                                      "<body>Don't forget me this weekend!</body>" +
                                                      "</note>");
            ServiceReference1.SendXmlWcfServiceClient client = new ServiceReference1.SendXmlWcfServiceClient();

            Console.WriteLine(client.GetData(builder.ToString()));

            Console.ReadLine();

        }
    }
}


2015年6月30日 星期二

[C#]計算Tiff檔頁數,使用BitMiracle.LibTiff.Net

用NuGet下載BitMiracle.LibTiff.Net,Version:2.4.511.0
官方網頁

原始碼如下
using BitMiracle.LibTiff.Classic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Calculate_Tiff_page_with_BitMiracle.LibTiff.Net
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"C:\Users\owlin\Downloads\test.tif";
            using (Tiff image = Tiff.Open(path, "r"))
            {
                Console.WriteLine(CalculatePageNumber(image));
                Console.ReadLine();
            }
        }
        private static int CalculatePageNumber(Tiff image)
        {
            int pageCount = 0;
            do
            {
                ++pageCount;
            } while (image.ReadDirectory());

            return pageCount;
        }
    }

}

[C#]FTP上下傳檔案,使用WinSCP

用NuGet下載WinSCP .Net Assembly,Version:5.7.4
官方網頁

原始碼如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinSCP;

namespace FTP_download_and_upload_with_WinSCP
{
    class Program
    {
        static string ftpUploadUrl = "IP或domain"; //不可以有ftp:\
        static string ftpAccount = "Ftp帳號";
        static string ftpPassword = "Ftp密碼";
        static string localPath = @"D:\temp\";
        static string remotePath = "/work/";

        static void Main(string[] args)
        {
            Download();
            Upload();
        }
        private static void Upload()
        {
            try
            {
                SessionOptions sessionOptions = new SessionOptions
                {
                    Protocol = Protocol.Ftp,
                    HostName = ftpUploadUrl,
                    UserName = ftpAccount,
                    Password = ftpPassword,
                };

                using (Session session = new Session())
                {
                    // Connect
                    session.Open(sessionOptions);

                    // Upload files
                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary;

                    TransferOperationResult transferResult;

                    transferResult = session.PutFiles(localPath + "*", remotePath, true, transferOptions); //*表示全部檔案

                    // Throw on any error
                    transferResult.Check();
                    // Print results
                    foreach (TransferEventArgs transfer in transferResult.Transfers)
                    {
                        Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
                    }
                    session.Close();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        private static void Download()
        {
            try
            {
                SessionOptions sessionOptions = new SessionOptions
                {
                    Protocol = Protocol.Ftp,
                    HostName = ftpUploadUrl,
                    UserName = ftpAccount,
                    Password = ftpPassword,
                };

                using (Session session = new Session())
                {
                    // Connect
                    session.Open(sessionOptions);

                    // Upload files
                    TransferOptions transferOptions = new TransferOptions();
                    transferOptions.TransferMode = TransferMode.Binary;

                    TransferOperationResult transferResult;

                    transferResult = session.GetFiles(remotePath + "*", localPath, false, transferOptions);//*表示全部檔案

                    // Throw on any error
                    transferResult.Check();
                    // Print results
                    foreach (TransferEventArgs transfer in transferResult.Transfers)
                    {
                        Console.WriteLine("Download of {0} succeeded", transfer.FileName);
                    }
                    session.Close();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}