Kelp-Space 是記錄一些生活雜事的Blog
如有任何程式設計的問題歡迎到 飛特技術論壇 討論

2008-10-25

如何學習程式設計

筆者從接觸程式到現在已經有五年了,這段期間碰過很多學習程式設計的人,大部分的人經過五年後還是差不多,但有一些人已經會做很危險的東西。功力的強弱不是看接觸多久,主要是看進步的速度,就像剛剛所說的,大部分的人花了五年的時間,卻達不到少數人一年所成長的功力。就科技產業而言,時間是很重要的,等到你龜速成長完畢後其他人不知道已經賺多少錢了,而且這些已經在賺錢的人不會成長嗎?很多人會問"學程式設計的秘訣是什麼?",其實筆者也不太清楚,如果硬要說的話天分是其中的一環。但光有天分是沒用的,這些成長速度很快的人一定有段時間是花大量時間在程式設計上,而成長速度很慢的人這時候通常在打電動,因為網路遊戲實在太令人著迷了。



第一代語言 - 機器語言
第二代語言 - 組合語言
第三代語言 - 程序導向語言 (ex:C語言)
第四代語言 - 物件導向語言 (ex:.NET)
第五代語言 - 問題導向語言或其他語言 (尚未普遍化)
最終語言 - 自然語言

目前這個世界已經進入第四代語言,物件導向語言。但在學術界還有很多老師停留在第三代語言。即使老師有跟上世界的腳步,教的是第四代語言,但是等到學生畢業後,主流語言還是第四代嗎?程式語言的最終目標就是自然語言,也就是要將程式簡單化,要讓開發時間縮短,目前軟體設計的難度跟以前比起來是簡單很多的。新一代語言一定有上一代語言的優點,如:撰寫容易、維護容易......等。這就是舊的語言一直在淘汰的原因,人們發現了更好用的語言後放棄了先前使用的語言。在學生階段,修了一堆語言,如:C、C++、JAVA、Basic......等,有很多人還會說"學了一堆語言,可是樣樣不精通",修了這麼多語言重點不是要你精通他,因為這些語言要成為吃飯工具的機會不高,學術界使用的語言很多都是過時的,學習這麼多語言的目的是要讓你學會"如何使用新的語言",這就是為什麼有人說"老師教的只有皮毛而已"。"這個語言我沒學過,所以沒辦法寫"大大們可能有聽過類似的話吧,如果學會如何使用新語言的人可能會說"這個語言我沒學過,可能要花一些時間",有些人使用新接觸的語言來設計簡單的木馬花不到兩星期就完成,筆者認為這才是真的"會寫程式"。


每種語言都有他的優缺點及限制,因此會有人說學某某語言才是對的,筆者認為學什麼語言都沒關係,因為你要做的事情不一定跟其他人一樣,而且對於會寫程式的人而言換語言的速度是很快的,所以慣用語言選擇能完成你想做的軟體即可,另外盡量挑選比較多人用的語言,這樣找資料會方便一點。


對於程式學習者來說,做一個自己想要的軟體是進步最快的方法,在做之前要謹慎的選擇適當的語言,這時候不要受限於學校教的語言。做3D遊戲的話盡量不要使用VB6,做木馬的話可以考慮VB6或delphi。


最近看到有人問說要學什麼語言所以才打這篇文章,希望本文能給想學程式設計的人一些解答,IT這條路是很累的......
迷:考試都快搞不定了還打文章

Sample Programs Using Asynchronous Sockets

Now that you have studied all the pieces, it’s time to put them together and create a set of real Windows asynchronous network programs. The following two programs recreate the SimpleTcpSrvr and SimpleTcpClient programs (Listings 5. 1 and 5.2, respectively) introduced in Chapter 5, "Connection-Oriented Sockets," but this time they use asynchronous network methods.

The Client Program
The AsyncTcpClient.cs program (Listing 8.2) uses the .NET Windows Forms library to create a Windows GUI environment for a simple TCP client. In a Windows environment, it is important that the program respond to Windows events from the user, as well as from the network


Listing 8.2: The AsyncTcpClient.cs program 複製程式碼(Copy to clipboard)
using System;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
class AsyncTcpClient Form:
{
  private TextBox newText;
  private TextBox conStatus;
  private ListBox results;
  private Socket client;
  private byte[] data = new byte[1024];
  private int size = 1024;
  public AsyncTcpClient()
  {
   Text = "Asynchronous TCP Client";
   Size = new Size(400, 380);
   
   Label label1 = new Label();
   label1.Parent = this;
   label1.Text = "Enter text string:";
   label1.AutoSize = true;
   label1.Location = new Point(10, 30);
   newText = new TextBox();
   newText.Parent = this;
   newText.Size = new Size(200, 2 * Font.Height);
   newText.Location = new Point(10, 55);
   results = new ListBox();
   results.Parent = this;
   results.Location = new Point(10, 85);
   results.Size = new Size(360, 18 * Font.Height);
   Label label2 = new Label();
   label2.Parent = this;
   label2.Text = "Connection Status:";
   label2.AutoSize = true;
   label2.Location = new Point(10, 330);
   conStatus = new TextBox();
   conStatus.Parent = this;
   conStatus.Text = "Disconnected";
   conStatus.Size = new Size(200, 2 * Font.Height);
   conStatus.Location = new Point(110, 325);
   Button sendit = new Button();
   sendit.Parent = this;
   sendit.Text = "Send";
   sendit.Location = new Point(220,52);
   sendit.Size = new Size(5 * Font.Height, 2 * Font.Height);
   sendit.Click += new EventHandler(ButtonSendOnClick);
   Button connect = new Button();
   connect.Parent = this;
   connect.Text = "Connect";
   connect.Location = new Point(295, 20);
   connect.Size = new Size(6 * Font.Height, 2 * Font.Height);
   connect.Click += new EventHandler(ButtonConnectOnClick);
   Button discon = new Button();
   discon.Parent = this;
   discon.Text = "Disconnect";
   discon.Location = new Point(295,52);
   discon.Size = new Size(6 * Font.Height, 2 * Font.Height);
   discon.Click += new EventHandler(ButtonDisconOnClick);
  }
  void ButtonConnectOnClick(object obj, EventArgs ea)
  {
   conStatus.Text = "Connecting...";
   Socket newsock = new Socket(AddressFamily.InterNetwork,
              SocketType.Stream, ProtocolType.Tcp);
   IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
   newsock.BeginConnect(iep, new AsyncCallback(Connected), newsock);
  }
  void ButtonSendOnClick(object obj, EventArgs ea)
  {
   byte[] message = Encoding.ASCII.GetBytes(newText.Text);
   newText.Clear();
   client.BeginSend(message, 0, message.Length, SocketFlags.None,
          new AsyncCallback(SendData), client);
  }
  void ButtonDisconOnClick(object obj, EventArgs ea)
  {
   client.Close();
   conStatus.Text = "Disconnected";
  }
  void Connected(IAsyncResult iar)
  {
   client = (Socket)iar.AsyncState;
   try
   {
     client.EndConnect(iar);
     conStatus.Text = "Connected to: " + client.RemoteEndPoint.ToString();
     client.BeginReceive(data, 0, size, SocketFlags.None,
            new AsyncCallback(ReceiveData), client);
   } catch (SocketException)
   {
     conStatus.Text = "Error connecting";
   }
  }
  void ReceiveData(IAsyncResult iar)
  {
   Socket remote = (Socket)iar.AsyncState;
   int recv = remote.EndReceive(iar);
   string stringData = Encoding.ASCII.GetString(data, 0, recv);
   results.Items.Add(stringData);
  }
  void SendData(IAsyncResult iar)
  {
   Socket remote = (Socket)iar.AsyncState;
   int sent = remote.EndSend(iar);
   remote.BeginReceive(data, 0, size, SocketFlags.None,
          new AsyncCallback(ReceiveData), remote);
  }
  public static void Main()
  {
   Application.Run(new AsyncTcpClient());
  }
}

The class constructor creates the window objects for the program. For this simple client program, the following objects are created:

A Label object and a TextBox object; these allow the customer to input text messages to send to the remote host

A ListBox object to display the messages returned by the remote host

A Label object and a TextBox object to display the connection status of the client program

Three Button objects; one for establishing a new TCP connection to a remote host, one for sending the text message and receiving a reply, and one for disconnecting the TCP session.

Each object is placed using the Point class and sized using the Size class; both of these classes are from the System.Drawing namespace. You can experiment with these values to get a feel for manually locating the objects on the Window form.

Note  If you have one of the Microsoft Visual products, such as Visual Studio or Visual C#, you can place all the Window objects using the graphical editing environment and allow the Form code to be generated automatically for you.


The Client Program Flow
You may notice that there is no network programming code at all in the class constructor. All of the network functions happen within EventHandler() methods. To get the AsyncTcpClient program to perform the same way as the original SimpleTcpClient program, you must first determine how to link the program protocol with the asynchronous events that will be triggered in the program. Then you must program them into the Windows event code methods. The diagram in Figure 8.3 shows each of the required methods and how they interact with the Windows event code. The following paragraphs describe the steps accomplished by the sample client program: connecting, receiving, sending, and disconnecting.



Connecting
As seen in the figure, when the customer clicks the Connect button, the method used to start the connection is performed. In the program code, this is the ButtonConnectOnClick() method:

ButtonConnectOnClick() 複製程式碼(Copy to clipboard)
void ButtonConnectOnClick(object obj, EventArgs ea)
{
  conStatus.Text = "Connecting...";
  Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
   ProtocolType.Tcp);
IPEndPoint iep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
  newsock.BeginConnect(iep, new AsyncCallback(Connected), newsock);
}

The ButtonConnectOnClick() method creates a new Socket object for the communication. Then it starts the BeginConnect() method with the address information of the remote host, the name of the associated AsyncCallback method, and the newly created Socket object. When this method finishes processing, Windows waits for the BeginConnect() method to trigger its event, which indicates that a connection is established with the remote host. When this occurs, program control is passed to the Connected() method:

Connected() 複製程式碼(Copy to clipboard)
void Connected(IAsyncResult iar)
{
  client = (Socket)iar.AsyncState;
  try
  {
   client.EndConnect(iar);
   conStatus.Text = "Connected to: " + client.RemoteEndPoint.ToString();
   client.BeginReceive(data, 0, size, SocketFlags.None,
    new AsyncCallback(ReceiveData), client);
  } catch (SocketException)
  {
   conStatus.Text = "Error connecting";
  }
}

The first thing that must be done in the Connected() method is to retrieve the original Socket object used for the connection. The AsyncState property of the IAsyncResult class returns the object passed to the AsyncCallback method, which in the case of the BeginConnect() method was the newly created Socket object.

After the Socket object is recreated, the EndConnect() method can be performed. Because it is possible that the EndConnect() method could fail (for instance, if the remote host is unavailable), it is a good idea to place it in a try-catch block. Should EndConnect() fail, you can notify the customer through the conStatus TextBox object.

In this program protocol, the first thing the server does after the connection is established is to send a welcome banner. To accommodate this, the AsyncTcpClient program must be prepared to accept an incoming message immediately after establishing the connection. This is done by using a BeginReceive() method call at the end of the Connected() method.

Receiving Data
After a new connection, and after every sent message, a ReceiveData() method is performed. The BeginReceive() method declares the ReceiveData() method, the AsyncCallback method that’s used when the data is received:

ReceiveData() 複製程式碼(Copy to clipboard)
void ReceiveData(IAsyncResult iar)
{
  Socket remote = (Socket)iar.AsyncState;
  int recv = remote.EndReceive(iar);
  string stringData = Encoding.ASCII.GetString(data, 0, recv);
  results.Items.Add(stringData);
}

Again, the first statement recreates the communication socket. After the original socket is recreated, the EndReceive() method is used, referencing the original IAsyncResult object, which pairs it with the original BeginReceive() method call. When the message is received from the remote host, it is placed in the data buffer referenced in the BeginReceive() method call. Because the data variable was defined as a global variable, it is used within this method to place the message in the results ListBox.

Sending Data
After entering a message in the TextBox object to send to the remote host, the customer clicks the Send button, and the SendData() method is performed. The EventHandler for the button points to the ButtonSendOnClick() method:

ButtonSendOnClick() 複製程式碼(Copy to clipboard)
void ButtonSendOnClick(object obj, EventArgs ea) { byte[] message = Encoding.ASCII.GetBytes(newText.Text); newText.Clear(); client.BeginSend(message, 0, message.Length, SocketFlags.None, new AsyncCallback(SendData), client); }

The message is extracted from the TextBox object, converted to a byte array, and sent out the socket. Because the ButtonSendOnClick() method does not have the original Socket object passed to it, it needs to use a class member to reference the connected socket. The BeginSend() method is used on the connected socket. It specifies the message and the AsyncCallback method to call when the BeginSend() function is ready to complete, along with an object to pass to the EndSend() method. In this case, it is the connected socket.

The AsyncCallback method, SendData(), is triggered when the socket indicates it is ready to send the message:

SendData() 複製程式碼(Copy to clipboard)
void SendData(IAsyncResult iar)
{
  Socket remote = (Socket)iar.AsyncState;
  int sent = remote.EndSend(iar);
  remote.BeginReceive(data, 0, size, SocketFlags.None,
   new AsyncCallback(ReceiveData), remote);
}

Once again, you see the familiar behavior: the original connected socket is recreated using the AsyncState of the state object passed from the BeginSend() method. When the original socket is recreated, the EndSend() method completes the data transmission. The IAsyncResult object pairs the EndSend()to the original BeginSend() and EndSend()returns the number of bytes that were actually sent out from the socket.

In this application, after a message is sent, the server is expected to echo it back. Knowing this, the BeginReceive() method is called to start the receiving process. BeginReceive()again calls the same ReadData() method, just as it did to receive the original connection’s welcome banner.

Disconnecting
Now that you have the wonders of event programming at your disposal, you can finally create a button to control the disconnect from the remote host (rather than having to define a control word to stop the client). Here’s how that works:

ButtonDisconOnClick() 複製程式碼(Copy to clipboard)
void ButtonDisconOnClick(object obj, EventArgs ea)
{
  client.Close();
  conStatus.Text = "Disconnected";
}

By issuing the Close() method for the client socket, the connection is disconnected. To establish a new connection, the customer can click the Connect button.

Warning  There are lots of things that can go wrong with this simple client example, including the customer’s clicking the wrong button at the wrong time. A real-world production application should check each time a button is clicked to ensure that the function can be performed.

The Server Program

Now that you have moved the client program into the Windows world, it is time to tackle the server program. Listing 8.3 shows the AsyncTcpSrvr.cs program, which mimics the functionality of the SimpleTcpSrvr program (Listing 5.1) from Chapter 5 but uses a Windows Forms environment and asynchronous sockets.

Listing 8.3: The AsyncTcpSrvr.cs program 複製程式碼(Copy to clipboard)
using System;
using System.Drawing;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
class AsyncTcpSrvr    Form:
{
  private TextBox conStatus;
  private ListBox results;
  private byte[] data = new byte[1024];
  private int size = 1024;
  private Socket server;
  public AsyncTcpSrvr()
  {
   Text = "Asynchronous TCP Server";
   Size = new Size(400, 380);
   results = new ListBox();
   results.Parent = this;
   results.Location = new Point(10, 65);
   results.Size = new Size(350, 20 * Font.Height);
   Label label1 = new Label();
   label1.Parent = this;
   label1.Text = "Text received from client:";
   label1.AutoSize = true;
   label1.Location = new Point(10, 45);
   Label label2 = new Label();
   label2.Parent = this;
   label2.Text = "Connection Status:";
   label2.AutoSize = true;
   label2.Location = new Point(10, 330);
   conStatus = new TextBox();
   conStatus.Parent = this;
   conStatus.Text = "Waiting for client...";
   conStatus.Size = new Size(200, 2 * Font.Height);
   conStatus.Location = new Point(110, 325);
   Button stopServer = new Button();
   stopServer.Parent = this;
   stopServer.Text = "Stop Server";
   stopServer.Location = new Point(260,32);
   stopServer.Size = new Size(7 * Font.Height, 2 * Font.Height);
   stopServer.Click += new EventHandler(ButtonStopOnClick);
   server = new Socket(AddressFamily.InterNetwork,
          SocketType.Stream, ProtocolType.Tcp);
   IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
   server.Bind(iep);
   server.Listen(5);
   server.BeginAccept(new AsyncCallback(AcceptConn), server);
  }
  void ButtonStopOnClick(object obj, EventArgs ea)
  {
   Close();
  }
  void AcceptConn(IAsyncResult iar)
  {
   Socket oldserver = (Socket)iar.AsyncState;
   Socket client = oldserver.EndAccept(iar);
   conStatus.Text = "Connected to: " + client.RemoteEndPoint.ToString();
   string stringData = "Welcome to my server";
   byte[] message1 = Encoding.ASCII.GetBytes(stringData);
   client.BeginSend(message1, 0, message1.Length, SocketFlags.None,
         new AsyncCallback(SendData), client);
  }
  void SendData(IAsyncResult iar)
  {
   Socket client = (Socket)iar.AsyncState;
   int sent = client.EndSend(iar);
   client.BeginReceive(data, 0, size, SocketFlags.None,
         new AsyncCallback(ReceiveData), client);
  }
  void ReceiveData(IAsyncResult iar)
  {
   Socket client = (Socket)iar.AsyncState;
   int recv = client.EndReceive(iar);
   if (recv == 0)
   {
     client.Close();
     conStatus.Text = "Waiting for client...";
     server.BeginAccept(new AsyncCallback(AcceptConn), server);
     return;
   }
   string receivedData = Encoding.ASCII.GetString(data, 0, recv);
   results.Items.Add(receivedData);
   byte[] message2 = Encoding.ASCII.GetBytes(receivedData);
   client.BeginSend(message2, 0, message2.Length, SocketFlags.None,
          new AsyncCallback(SendData), client);
  }
  public static void Main()
  {
   Application.Run(new AsyncTcpSrvr());
  }
}

Just as the client program does, the AsyncTcpSrvr program uses basic Windows Forms objects to create the customer graphical environment:

A Label object and a ListBox object to identify the data received from the connected client

A Label object and a TextBox object to display the status of the socket and the connected client

A Button object to allow the customer to stop the server and exit the program

After creating the customer interface, you must code the network programming part. Unlike the client program, the AsyncTcpSrvr program includes network programming code that must be done in the class constructor.

The Server Program Flow

Like its counterpart SimpleTcpSrvr in Chapter 5, the AsyncTcpSrvr program must follow a set procedure for communicating with remote clients. Figure 8.5 diagrams the steps required for the server.


Waiting for New Client Connections
Because you know that the server must immediately listen for incoming connections, the required network programming code is added to the class constructor after the Forms objects have been created:

new Socket() 複製程式碼(Copy to clipboard)
server = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
server.Bind(iep);
server.Listen(5);
server.BeginAccept(new AsyncCallback(AcceptConn), server);

By now, you should recognize this as standard server socket code: creating a new Socket object, binding it to a local IPEndPoint object, and listening for new connection attempts.

The BeginAccept() method specifies the AsyncCallback method to use when a connection is received, and the object to pass to the AsyncCallback method.

When a connection attempt is detected, the AsyncCallback method registered with the BeginAccept() method is performed:

AcceptConn() 複製程式碼(Copy to clipboard)
void AcceptConn(IAsyncResult iar)
{
  Socket oldserver = (Socket)iar.AsyncState;
  Socket client = oldserver.EndAccept(iar);
  conStatus.Text = "Connected to: " + client.RemoteEndPoint.ToString();
  string stringData = "Welcome to my server";
  byte[] message1 = Encoding.ASCII.GetBytes(stringData);
  client.BeginSend(message1, 0, message1.Length, SocketFlags.None,
   new AsyncCallback(SendData), client);
}

The AsyncCallback method recreates the original Socket object using the AsyncState property of the IAsyncResult object. When the EndAccept() method is called, it uses the IAsyncResult object to pair it up with the calling BeginAccept() method. The EndAccept() method returns a new Socket object to be used for all communication with the remote client.

As reflected in the program flow diagram, the next step for the server is to send a welcome banner to the client. This requires the BeginSend() method, which registers an AsyncCallback method to complete the send operation when the socket is ready to send out data.

Warning  Note that the Socket object passed to the AsyncCallback method is the newly created client socket, not the original server socket. This is the socket that is connected to the remote client, and it should be used for all communications with that client.

Sending Data
The EndSend() method is found in the AsyncCallback method registered in the BeginSend() method:

SendData() 複製程式碼(Copy to clipboard)
void SendData(IAsyncResult iar)
{
  Socket client = (Socket)iar.AsyncState;
  int sent = client.EndSend(iar);
  client.BeginReceive(data, 0, size, SocketFlags.None,
   new AsyncCallback(ReceiveData), client);
}

This sample program uses the standard EndSend() format. A good-quality production program would also compare this value with the original byte count of the sent message and perform the BeginSend() method again if some of the message were not properly sent.

The server program flow model specifies that after the welcome banner is sent, the server should wait for an incoming message from the client. This is accomplished with the BeginReceive() method, which specifies a data buffer in which to place the received data, along with the AsyncCallback method to call when the data is received.

Receiving Data
The EndReceive() method is placed in the AsyncCallback method for the BeginReceive() method:

ReceiveData() 複製程式碼(Copy to clipboard)
void ReceiveData(IAsyncResult iar)
{
  Socket client = (Socket)iar.AsyncState;
  int recv = client.EndReceive(iar);
  if (recv == 0)
  {
   client.Close();
   conStatus.Text = "Waiting for client...";
   server.BeginAccept(new AsyncCallback(AcceptConn), server);
   return;
  }
  string receivedData = Encoding.ASCII.GetString(data, 0, recv);
  results.Items.Add(receivedData);
  byte[] message2 = Encoding.ASCII.GetBytes(receivedData);
  client.BeginSend(message2, 0, message2.Length, SocketFlags.None, new AsyncCallback(SendData), client);
}

The ReceiveData() method is a little lengthier than the others because it has to accommodate two scenarios in the program flow. First, as always, the client socket is recreated using the standard AsyncState property. Next, the EndReceive() method is called using the IAsyncResult object to pair it up with the appropriate BeginReceive() method. EndReceive()returns the number of bytes received from the socket. As stated earlier, a real-time production program would check the number of bytes received against an expected message size.

If the received message size is zero bytes, it is assumed that the remote client has disconnected the TCP session. In that case, the client socket is closed, and the server can start listening for a new client connection by using the BeginAccept() method and starting the whole program flow all over again.

If a message has been received from the remote client, it must be displayed in the ListBox object and echoed back to the client. This is done by using the BeginSend() method again. Because the program flow now duplicates the original flow from sending the welcome banner, the original SendData() method is used for this AsyncCallback method as well.

取自C# Network Programing

霹靂燈 - 使用右移減少燈數

某天在看加強版遊戲王的時候MSN突然傳來求救訊息。
順道把這個功能的程式PO上來。
這個功能怎麼想都只能想到這麼爛的標題。
直接看功能要求。
使用晶片為8051。


功能要求 複製程式碼(Copy to clipboard)
●●●●●●●
●●●●●●○
●●●●●○●
●●●●●○○
●●●●○●○
●●●●○○●
●●●●○○○
●●●○●○○
●●●○○●○
●●●○○○●
●●●○○○○
●●○●○○○
●●○○●○○
●●○○○●○
●●○○○○●
●●○○○○○
●○●○○○○
●○○●○○○
●○○○●○○
●○○○○●○
●○○○○○●
●○○○○○○
○●○○○○○


程式碼 複製程式碼(Copy to clipboard)
#include <REGX51.H>

void delay(void)
{
        unsigned long m;
        for(m=0;m<2000;m++);
}

int main(void)
{
        int i,j,x;
        while(1)
        {
                i=0xff;
                j=1;
                P2=i;
                delay();

                while(i>0)      //第一層迴圈   用來減少霹靂燈數量
                {
                        i<<=1;
                        j<<=1;
                        x=j;     //設定右移霹靂燈起始位置
                        while(x>1)     //第二層迴圈 用來右移霹靂燈
                        {
                                x>>=1;
                                P2=i|x;
                                delay();
                        }
                        P2=i;
                        delay();
                }
        }
}

Ark - Data Backuper 緩備份軟體 (開源碼 1.0.0.4)

【軟體名稱】:Ark - Data Backuper
【軟體分類】:檔案備份
【軟體性質】:開源軟體
【檔案大小】:566kB
【放置空間】:Phate
【軟體介紹】:
/// ///////////////////////////////////
/// 專案名稱:Ark - Data Backuper
///
/// 開始日期:2008-09-01
/// 結束日期:2008-10-12
///
/// 開發者:
/// Kelp[at]phate.tw (Kelp @ Phate Technology)
///
/// 開發語言:
/// Visual C# 2008
///
/// 用途:
/// 主要提供緩備份功能
///
///
/// 記錄:
/// 2008-10-12
/// 1.0.0 版完成
/// 1.0.0.1 修正每日一次時間 更改為24小時制
///
/// 2008-10-13
/// 1.0.0.2 檔案容量不同時不可進行覆蓋,只有最後修改時間較新時才能覆蓋
/// 1.0.0.2 啟動後記錄先前視窗大小
/// 1.0.0.3 Standby時消除狀態bar
///
/// 2008-10-14
/// 1.0.0.4 對開機讀不到資料的部分進行修正
/// 1.0.0.4 系統開啟程式時自動隱藏視窗
///
/// ///////////////////////////////////





此軟體使用少量的系統資源進行檔案複製,能夠把A資料底下的所有檔案夾拷貝到B資料夾中。
使用者可以選擇固定的時間進行備份。
其實是在下硬碟太多顆了,整理起來真的不是一般的糟糕 (誤。
硬碟裡的程式每天都會改一些,所以備份已經做到很煩了,就用這隻軟體來方便方便吧。 XD
開源是為了讓程式進步得更快,如有任何BUG可以通知我們,或自行修改程式碼(修改後希望能通知我們)。
我們也歡迎開發亞種Ark。

執行檔載點
程式碼載點

抓取檔案的ICON

抓取檔案的ICON程式碼 複製程式碼(Copy to clipboard)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestProject
{
    public partial class Form1 : Form
    {
        [System.Runtime.InteropServices.DllImport("shell32")]
        private static extern IntPtr ExtractAssociatedIcon(IntPtr hInst, string lpIconPath, ref int lpiIcon);
        private IntPtr hIcon;

        public Form1()
        {
            InitializeComponent();

            this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

            int iIconIndex = 0;
            hIcon = ExtractAssociatedIcon(this.Handle, "D:\\123.jpg", ref iIconIndex);
        }

        private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Icon icon = Icon.FromHandle(hIcon);
            e.Graphics.DrawIcon(icon, 10, 10);
        }
    }
}

抓取短檔名

C#是使用unicode所以遇到日文或其他語言的路徑都不太會發生問題。
但使用了.net framework以外的東西,傳了其他語言的路徑時有可能出錯。

筆者而在.net framework裡沒找到轉shortPath的函數,所以只好調用API....
如果.net framework可以直接轉的話就請大大PO上來了。


抓取短檔名 複製程式碼(Copy to clipboard)
using System.Runtime.InteropServices;   //調用API

        //在class內
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern int GetShortPathName([MarshalAs(UnmanagedType.LPTStr)] string path, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder shortPath, int shortPathLength);

        //在事件內程式碼
        StringBuilder shortPath = new StringBuilder(255);
        GetShortPathName("C:\test測試.txt", shortPath, shortPath.Capacity);

進制轉換簡介

//十進制轉二進制
Console.WriteLine(Convert.ToString(69, 2));
//十進制轉八進制
Console.WriteLine(Convert.ToString(69, 8));
//十進制轉十六進制
Console.WriteLine(Convert.ToString(69, 16));

//二進制轉十進制
Console.WriteLine(Convert.ToInt32(”100111101″, 2));
//八進制轉十進制
Console.WriteLine(Convert.ToInt32(”76″, 8));
//十六進制轉十進制
Console.WriteLine(Convert.ToInt32(”FF”, 16));