2011年12月29日 星期四

C#中如何得到webbrowser里url中的参数

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;
namespace WindowsFormsApplication2{
    
public partial class Form1 : Form
    
{
        
public Form1()
        
{
            
InitializeComponent();
        
}

        
private void Form1_Load(object sender, EventArgs e)
        
{
            
webBrowser1.Navigate("http://www.abc.com/news.aspx?class=111111&cid=22222222");
        
}

        
private void button1_Click(object sender, EventArgs e)
        
{
            
string url = webBrowser1.Url.Query.ToString(); //獲取地址的參數
            
url = url.Replace("?", ""); //去除第一個參數的問號
            
string[] urlParam = url.Split('&'); //根據參數間的& 號獲取參數數組
            
foreach (string s in urlParam)
            
{
                
string[] val = s.Split('='); //將參數名與參數值賦值給數組
                
MessageBox.Show("參數名稱為:" + val[0] + " 參數值為:" + val[1]);
            
}
        
}
    
}}

Flash Socket安全策略及應對方法

Flash Socket安全策略<policy-file-request/>及應對方法

在Adobe Flash Player 升級到9.0.124 後,由於安全策略更改,原來Socket 或XmlSocket 的應用裡的http 方式加載安全策略的手段不能繼續使用。更改如下:

1, 首先檢測目標服務器的843 端口是否提供安全策略2, 如果1 沒有檢測到策略,則檢測actionscript 是否使用了Security.loadPolicyFile(xmlsocket://)手段提供安全策略,如果還沒檢測到,則使用第3 步檢測3, 檢測目標服務器目標端口是否提供安全策略。


在說具體處理方式前,我先描述一下Flash Player 的驗證過程。在Flex 程序發出Socket 或XmlSocket( 以下統稱為Socket) 請求前, FlashPlayer 會先判斷是否為本地調用,如果不是。即用一個Socket 去鏈接到你的服務端,三次握手成功後一方面發出字符串“ <policy-file-request/>\0 “另一方面監聽返回的安全策略。安全策略接收成功後, FlashPlayer 就斷開驗證的Socket ,然後再運行程序本身的Socket 。在整個SWF 運行期間,無論你請求多少次,只要域相同, FlashPlayer 就只驗證一次。這裡有兩個重點:第一個是驗證的Socket 和程序的Socket 是兩個Socket 。所以你在本地測試時,服務端監聽到N 個Socket 請求,但佈置到服務端后,服務端會監聽到N+1 個請求。第二是驗證的Socket 發送“ <policy-file-request/>\0 “請求和接收你的策略文件是沒有先後關係的,所以你沒必要接收完“ <policy-file-request/>\0 “後才發策略文件。我的做法是只要監聽到請求,就把策略字符串發過去。

在Java中,我處理的方法是獨立啟動一個843端口的服務專門監聽授權請求,這樣客戶端響應速度很快,代碼片段如下:String xml = "<?xml version=\"1.0\"?><cross-domain-policy><site-control permitted-cross-domain-policies=\"all\"/><allow-access-from domain= \"*\" to-ports=\"*\"/></cross-domain-policy>\0";Socket socket=serverSocket.accept();BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));PrintWriter pw = new PrintWriter(socket.getOutputStream());char[] by = new char[22];br.read(by, 0, 22);String s = new String(by);System.out.println("s="+s);if (s.equals("<policy-file-request/>")) {System.out.println("接收policy-file-request");pw.print(xml);pw.flush();br.close();pw.close();socket.close();}另外,一定要注意xml 字符串後面的”\0” ,因為Flash Player 是用他來做分隔符的,如果你不加上,客戶端驗證socket 可能就一直等待。另外授權接收的socket一定要斷開。

Flash socket通訊中的安全策略問題詳解

無法查看此摘要。請 按這裡查看文章。

c# socket server for flash

   string xml = "<?xml version=\"1.0\"?><cross-domain-policy><site-control permitted-cross-domain-policies=\"all\"/><allow-access-from domain=\"*\" to-ports=\"*\"/></cross-domain-policy>\0";

 if (msg.IndexOf("<policy-file-request/>") != -1)
                {
                    cb.SendMsg(xml, this.mTcpClient);
                }

Java socket server for flash

package sban.socketServer;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URL;
/**
 * sban policy server for flex/flash socket
 *
 * @author sban
 *
 */
public class SbanSocketPolicyServer {
 public SbanSocketPolicyServer() {
 }
 public static String readFileAsString(String url)
   throws java.io.IOException {
  byte[] buffer = new byte[(int) new File(url).length()];
  BufferedInputStream f = new BufferedInputStream(
    new FileInputStream(url));
  f.read(buffer);
  f.close();
  return new String(buffer);
 }
 public static void main(String[] args) throws IOException {
  ServerSocket server = new ServerSocket();
  SocketAddress endpoint = new InetSocketAddress("127.0.0.1", 843);
  server.bind(endpoint);
  String path = SbanSocketPolicyServer.class.getResource( "crossdomain.xml" ).getPath();
  System.out.println(path);
  String policy = readFileAsString( path );
  while (true) {
   System.out.println("wait for client...");
   Socket socket = server.accept();
   BufferedReader in = new BufferedReader(new InputStreamReader(
     socket.getInputStream()));
   PrintWriter out = new PrintWriter(socket.getOutputStream());
   String s = in.readLine();
   System.out.println(s);
   if (s.indexOf("policy-file-request") > -1) {
    out.print(policy + '\0');
    out.flush();
    in.close();
    out.close();
    System.out.println("sended policy context.");
    socket.close();
   }
  }
 }
}


crossdomain.xml文件

<cross-domain-policy>
<site-control permitted-cross-domain-policies="all"/>
<allow-access-from domain="*" to-ports="*" />
</cross-domain-policy>

Java int to String(整數轉字串)

Java int to String(整數轉字串)
  1. String stringValue = Integer.toString(12345);
  2. String stringValue = String.valueOf(12345);
  3. String stringValue = new String(""+12345);
 
 
Java string to int(字串轉整數)
  1. int intValue = Integer.valueOf("12345");
  2. int intValue = Integer.parseInt("12345");

2011年12月28日 星期三

android unable to open sync connection

這是Device資源不足

最後最終解法是
1.重新建立連線,可以利用關閉手機上的[開發]->[USB除錯中]
2.拔開USB線
3.將所有手機中開啟的程式關閉
4.打開[USB除錯中]
5.將USB線接上,一切都OK了

2011年12月25日 星期日

C# 處理表單層級的鍵盤輸入

此應用程式包含了 TextBox 以及其他幾個控制項,可讓您將焦點從 TextBox 身上移走。 FormKeyPress 事件會使用 '1'、'4' 和 '7',而 TextBoxKeyPress 事件則會使用 '2'、'5' 和 '8' 同時顯示其餘的按鍵。 請比較這兩種情況下的輸出:當您在 TextBox 具有焦點時按下了某個數字鍵所得到的 MessageBox 輸出,以及當您在焦點是位於其他任何一個控制項上時按下了某個數字鍵所得到的 MessageBox 輸出。


using System;
using System.Drawing;
using System.Windows.Forms;

namespace KeyboardInputForm
{ class Form1 : Form
    {
        TextBox TextBox1 = new TextBox();

        [STAThread]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }

        public Form1()
        {
            this.AutoSize = true;

            FlowLayoutPanel panel = new FlowLayoutPanel();
            panel.AutoSize = true;
            panel.FlowDirection = FlowDirection.TopDown;
            panel.Controls.Add(TextBox1);
            this.Controls.Add(panel);

            this.KeyPreview = true;
            this.KeyPress +=
                new KeyPressEventHandler(Form1_KeyPress);
            TextBox1.KeyPress +=
                new KeyPressEventHandler(TextBox1_KeyPress);
        }

        // Detect all numeric characters at the form level and consume 1,
        // 4, and 7. Note that Form.KeyPreview must be set to true for this
        // event handler to be called.
        void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= 48 && e.KeyChar <= 57)
            {
                MessageBox.Show("Form.KeyPress: '" +
                    e.KeyChar.ToString() + "' pressed.");

                switch (e.KeyChar)
                {
                    case (char)49:
                    case (char)52:
                    case (char)55:
                        MessageBox.Show("Form.KeyPress: '" +
                            e.KeyChar.ToString() + "' consumed.");
                        e.Handled = true;
                        break;
                }
            }
        }

        // Detect all numeric characters at the TextBox level and consume 
        // 2, 5, and 8.
        void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar >= 48 && e.KeyChar <= 57)
            {
                MessageBox.Show("Control.KeyPress: '" +
                    e.KeyChar.ToString() + "' pressed.");

                switch (e.KeyChar)
                {
                    case (char)50:
                    case (char)53:
                    case (char)56:
                        MessageBox.Show("Control.KeyPress: '" +
                            e.KeyChar.ToString() + "' consumed.");
                        e.Handled = true;
                        break;
                }
            }
        }
    }
}

2011年12月22日 星期四

php 使用 glob 搜尋所有檔案路徑

//以遞迴的方式,取得深層資料夾的所有路徑
function listdirs($dir) {
    static $alldirs = array();
    $dirs = glob($dir . '/*', GLOB_ONLYDIR);
    if (count($dirs) > 0) {
        foreach ($dirs as $d) $alldirs[] = $d;
    }
    foreach ($dirs as $dir) listdirs($dir);
    return $alldirs;
}

C# 判斷檔案是否存在

如何判斷檔案是否存在,可以透過
File.Exists 方法 : 判斷指定的檔案是否存在。
http://msdn.microsoft.com/zh-tw/library/system.io.file.exists(VS.80).aspx
在 MSDN 中有提出依些要注意的事情

Exists 方法不應用於路徑驗證,這個方法僅會檢查 path 中指定的檔案是否存在。將無效路徑傳遞至 Existsl 會傳回 false。
請注意,在您呼叫 Exists 方法並對檔案執行其他作業 (例如,Delete) 的期間內,其他處理序有可能對檔案進行某些動作。建議的程式設計作法是在如範例所示的 Try...Catch 區塊中,包裝 Exists 方法,以及您對檔案採取的作業,這有助於縮小潛在衝突的範圍。Exists 方法只能協助確保檔案可供使用,但不保證一定可用。
允許用 path 參數來指定相對或絕對路徑的資訊。相對路徑資訊會被解譯為與目前的工作目錄相對。若要取得目前的工作目錄,請參閱 GetCurrentDirectory。
如果 path 描述目錄,這個方法會傳回 false。判斷檔案是否存在之前,移除 path 參數後端的空白字元。

以下來判斷 test.txt 檔案是否存在

// 判斷 test.txt 檔案是否存在   
string FileName = "test.txt";    
if (System.IO.File.Exists(FileName))    
{    
   MessageBox.Show(FileName + " 檔案存在");    
}    
else    
{    
   MessageBox.Show(FileName + " 檔案不存在");    
}  


 

C# String 轉換成 Color; color轉換成string


string轉換成color
           string col = "#FF8400";
           this.BackColor = System.Drawing.ColorTranslator.FromHtml(col);
          
color轉換成string
           Color mycolor = this.BackColor;
            string strcol = System.Drawing.ColorTranslator.ToHtml(mycolor);
            MessageBox.Show(strcol);

C# 使用 ColorDialog 元件顯示色板

private void button1_Click(object sender, System.EventArgs e)
{
   if(colorDialog1.ShowDialog() == DialogResult.OK)
   {
      button1.BackColor = colorDialog1.Color;
   }
}

2011年12月21日 星期三

c# 多國語言實作

多國語言winform測試,
首先在在專案內新增FBRS.en.resxFBRS.zh-CN.resx二個語系資源檔
判斷使用者目前的語系Thread.CurrentThread.CurrentUICulture.Name;
最後進行資源檔取得的置換動作。

        /// <summary>
        /// 初始化UI多國語言介面
        /// </summary>
        private void InitUICulture()
        {
            string userUICulture = Thread.CurrentThread.CurrentUICulture.Name;
            if (userUICulture != "zh-TW")
            {
                switch (userUICulture)
                {
                    case "en":
                        this.ci = new CultureInfo("en");
                        break;
                    case "zh-CN":
                        this.ci = new CultureInfo("zh-CN");
                        break;
                }

                Thread.CurrentThread.CurrentUICulture = this.ci;
                //第一個參數:專案根目錄名稱.資源檔名(basename)
                //語系命名不用加
                ResourceManager rm = new ResourceManager("FlippingBookWizard.FBRS",
                                         Assembly.GetExecutingAssembly());

                this.lblLogin.Text = rm.GetString("lblLogin");
                this.lblPassword.Text = rm.GetString("lblPassword");
                this.lblLoginMsg.Text = rm.GetString("lblLoginMsg");
                this.btnLogin.Text = rm.GetString("btnLogin");
            }
        }

C# 鍵盤 KeyChar 值的取得

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            char Key_Char = e.KeyChar;//判斷按鍵的 Keychar
            MessageBox.Show(((int)(Key_Char)).ToString());//轉成整數顯示
        }



Key Char
1234567890Entera
495051525354555657581397

c#控制鍵盤

this.Form1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.AKeyDown);//從Form1抓取key事件


private void AKeyDown(object sender, KeyEventArgs e)
{
 switch (e.KeyCode)
 {
   //需要程式做的事情
   }
}

c# 如何做字串編碼的動作,BIG5 to UTF8

string a = "測試";
byte[] b=Encoding.Default.GetBytes(a);//將字串轉為byte[]
MessageBox.Show(Encoding.Default.GetString(b));//驗證轉碼後的字串,仍正確的顯示.
byte[] c = Encoding.Convert(Encoding.Default, Encoding.UTF8, b);//進行轉碼,參數1,來源編碼,參數二,目標編碼,參數三,欲編碼變數
MessageBox.Show(Encoding.UTF8.GetString(c));//顯示轉為UTF8後,仍能正確的顯示字串

C# 鍵盤的按鍵判斷

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
//判斷 textbox1 裡面是不是 按Enter(Enter = 13) ,如果是 才動作
if (e.KeyChar == 13)
{
button1.Focus();

}