搜尋此網誌

顯示具有 C# 標籤的文章。 顯示所有文章
顯示具有 C# 標籤的文章。 顯示所有文章

2011年12月6日 星期二

怪C# process start exe 的 Win7 權限不足?

以C#執行程式本身以外的執行檔時,若屬於登錄檔(regedit)裡找的到的檔案(如 notepad.exe、word檔等等),都能執行成功。
但若一個執行檔,在「所在地範圍外」執行,就出問題了。
執行程式本身以外的執行檔(exe),步驟如下:
1、老方法,以process來執行額外程序(using System.Diagnostics 先)。
2、加入以下程式碼
Process _exe = new Process(); _exe.StartInfo.FileName = "路徑"; _exe.StartInfo.WorkingDirectory = "所在資料夾"; _exe.Start(); 3、這裡的重點在於「_exe.StartInfo.WorkingDirectory = "所在資料夾";」。若少了這一個動作,執行檔常常會無法正常執行。

2010年3月18日 星期四

C#:Dynamic Array

適用於 .NET 2.0 以上

無法確定陣列長度,要動態存取Grid陣列時:

List<Grid> dynamicGrid = new List<Grid>();

加入物件:

Grid myGrid = new Grid();
dynamicGrid.Add(myGrid)

取得物件,假設想要取得第一萬兩千三百四十五個Grid:

Grid tmpGrid = dynamicGrid[12345];


參考頁:http://forums.asp.net/p/1188478/2036031.aspx#2036031

2010年3月1日 星期一

Silverlight、C#:Unicode轉換

在C#中可使用以下程式碼對字串轉換為Unicode或將Unicode轉換為字元:

Encode

using System.Text;

private string StringToUnicode(string srcText)
{
  string dst = "";
  char[] src = srcText.ToCharArray();
  for (int i = 0; i < src.Length; i++)
  {
   byte[] bytes = Encoding.Unicode.GetBytes(src[i].ToString());
   string str = @"\u" + bytes[1].ToString("X2") + bytes[0].ToString("X2");
   dst += str;
  }
  return dst;
}

Decode

using System.Globalization;

private string UnicodeToString(string srcText)
{
string dst = "";
string src = srcText;
int len = srcText.Length / 6;

for (int i = 0; i <= len - 1; i++)
{
  string str = "";
  str = src.Substring(0, 6).Substring(2);
  src = src.Substring(6);
  byte[] bytes = new byte[2];
  bytes[1] =  
   byte.Parse(int.Parse
    (str.Substring(0,2),NumberStyles.HexNumber).ToString());
  bytes[0] = byte.Parse(int.Parse
    (str.Substring(2,  2),NumberStyles.HexNumber).ToString());
  dst += Encoding.Unicode.GetString(bytes);
  }
  return dst;
}


但上述程式碼中,Decode的部份在Slilverlight會出現「... is inaccessable due to its protection level」的錯誤,請改為以下內容,程式即可正常編譯:
Decode at Silverlight

string UnicodeToString(string srcText)
{
 string dst = "";
 string src = srcText;
 int len = srcText.Length / 6;

 for (int i = 0; i <= len - 1; i++)
 {
  string str = "";
  str = src.Substring(0, 6).Substring(2);
  src = src.Substring(6);
  byte[] bytes = new byte[2];
  bytes[1] = byte.Parse(int.Parse(str.Substring(0, 2),
   NumberStyles.HexNumber).ToString());
  bytes[0] = byte.Parse(int.Parse(str.Substring(2, 2),
   NumberStyles.HexNumber).ToString());

  Encoding enc = Encoding.GetEncoding("utf-16");
  dst += enc.GetString(bytes, 0, bytes.Length);

 }
  return dst;
}

2010年1月20日 星期三

Silverlight with C#:★★★ Attached Property ★★★

『對Silverlight元件增加額外的屬性』,在許多地方都非常之好用:
1.對專案新增一個 .cs 檔案:


在此對新增的Class命名為「addedProperty」


2.addProperty.cs最初內容為:

public class addProperty // class name : the property class
{
}

請加入以下內容:

public static readonly DependencyProperty addedMsg =
    DependencyProperty.RegisterAttached(
  //Name of the property
  "addedProperty",
  //Type of the property
  typeof( bool ),
  //Type of the provider of the registered attached property
  typeof( addProperty ),
  //Callback invoked in case the property value has changed
  null );
public static readonly DependencyProperty addedMsg2 =
    DependencyProperty.RegisterAttached(
  "addedProperty2",
  typeof(string),
  typeof(addProperty),
  null);


3.接著測試程式能否加入此Attached Property,先在主要的 .xmal檔中加入以下內容:

<Grid x:Name="LayoutRoot">
<TextBlock x:Name="myText" Text="message" Loaded="onLoad"/>
</Grid>


4.加入「onLoad」function,簡單的產生一個Silverlight元件以測試結果。:

private void onLoad(object sender, RoutedEventArgs e)
{
 myText.SetValue(addProperty.addedMsg, false);
 MessageBox.Show(myText.GetValue(addProperty.addedMsg).ToString());
 myText.SetValue(addProperty.addedMsg2, "my name is Creat");
 MessageBox.Show(myText.GetValue(addProperty.addedMsg2).ToString());
}


在Attached Property裡,第一個屬性為 bool,第二個屬性為 string,程式載入後,以MessageBox顯示我們在執行中加入的Property.你甚至可以加入C#中的Object或是Silverlight元件所屬頂層Object.

2009年12月15日 星期二

C#:Silper 運用--以指定tag拆解字串

string下的「Splite」方法,能夠指定特定的字元,將一個字串分解成一組字串陣列,例如以下程式內容會以MessageBox顯示a、b、c、d、e、f、g:

 string strData = "a;b;c;d;e;f;g";
string[] separator = new string[] { ";" };
 string[] strSplitArr =
  strData.Split(separator, StringSplitOptions.RemoveEmptyEntries);
 foreach (string arrStr in strSplitArr)
 {
  MessageBox.Show(arrStr);
 }

再者,以下內容能夠產生「Apple」、「Banana」、「Cantaloupe」:

 string[] separator = new string[] { "[", "]" };
 string[] strSplitArr =
  strData.Split(separator, StringSplitOptions.RemoveEmptyEntries);
 foreach (string arrStr in strSplitArr)
 {
  MessageBox.Show(arrStr);
 }

或者有必要時,使用[Fruit] + [/Fruit]來將字串拆解成「Apple」、「Banana」、「Cantaloupe」:

 string strData =
  "[Fruit]Apple[/Fruit][Fruit]Banana[/Fruit][Fruit]Cantalopue[/Fruit]";
 string[] separator = new string[] { "[Fruit]", "[/Fruit]" };
 string[] strSplitArr =
  strData.Split(separator, StringSplitOptions.RemoveEmptyEntries);
 foreach (string arrStr in strSplitArr)
 {
  MessageBox.Show(arrStr);
 }

2009年12月3日 星期四

Silverlight、C#:Post in HttpWebRequest

以下主要在從事Silverlight做Web Request,以Post對Server要求資料時用上:

 WebClient wc_DoLogin = new WebClient();
wc_DoLogin.UploadStringCompleted += new
   UploadStringCompletedEventHandler(wc_DoLogin_UploadStringCompleted);
 wc_DoLogin.Headers["content-type"]
   = "application/x-www-form-urlencoded";
 wc_DoLogin.UploadStringAsync(
  new Uri(Url, UriKind.Absolute),
  "POST",
  "欄位1=欄位值&欄位2=欄位值2");

Server回應將會在wc_DoLogin_UploadStringCompleted被接收:

void wc_DoLogin_UploadStringCompleted
(object sender, UploadStringCompletedEventArgs e)
{
MessageBox.Show("result is : " + e.Result);
}