2017-05-08 23 views
0

我有下面的C#代碼:C#編譯錯誤 - 沒有「主」方法適合

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Security.Cryptography; 

namespace command 
{ 
    class Program 
    { 

     public string DecryptStr(string _source, string _key) 
     { 
      string str; 
      try 
      { 
       byte[] bytes1 = Encoding.ASCII.GetBytes(_key.Substring(0, 8)); 
       byte[] bytes2 = Encoding.ASCII.GetBytes(_key.Substring(8, 8)); 
       DES des = (DES)new DESCryptoServiceProvider(); 
       des.Key = bytes1; 
       des.IV = bytes2; 
       byte[] buffer = new byte[_source.Length/2]; 
       for (int index = 0; index < _source.Length/2; ++index) 
       { 
        int int32 = Convert.ToInt32(_source.Substring(index * 2, 2), 16); 
        buffer[index] = (byte)int32; 
       } 
       MemoryStream memoryStream = new MemoryStream(); 
       CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, des.CreateDecryptor(), CryptoStreamMode.Write); 
       cryptoStream.Write(buffer, 0, buffer.Length); 
       cryptoStream.FlushFinalBlock(); 
       str = Encoding.Default.GetString(memoryStream.ToArray()); 
       memoryStream.Close(); 
      } 
      catch 
      { 
       str = "Key Error..."; 
      } 
      return str; 
     } 

     public void Main(string[] args) 
     { 
      string decrypted = this.DecryptStr(args[0], "0BDFC73BC56346AA"); 
      Console.WriteLine(decrypted); 
     } 
    } 
} 

我不知道任何C#,但似乎語法確定從一個紅寶石點/ Python程序員。不幸的是,當我嘗試編譯它時,我得到以下錯誤:

------ Build started: Project: Decrypt, Configuration: Debug Any CPU ------ 
CSC : error CS5001: Program 'c:\Users\John\Documents\Visual Studio 2012\Projects\Decrypt\Decrypt\obj\Debug\Decrypt.exe' does not contain a static 'Main' method suitable for an entry point 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 
+3

線索是錯誤消息:「不包含** **靜態'Main'法」 –

回答

2

主要方法應該是static。在你的代碼中不是。

創建一個控制檯應用程序時,請參閱例如默認生成:

namespace ConsoleApplication1 
{ 
    public class Program 
    { 
     static void Main(string[] args) 
     { 
     } 
    } 
} 

而且,你不能再使用this.DecryptStr,因爲它是一個靜態類。你有兩種選擇:

  1. 創建一個Program的實例,然後調用它的方法。
  2. 更改方法是靜態的太(但仍不能使用this
+0

是後來我得到這個:'關鍵字'this'在'this.DecryptStr'的靜態屬性,靜態方法或靜態字段初始值設定中無效,我應該如何在不帶this的情況下調用該函數? – bsteo

+0

使DecryptStr方法也是靜態的,並直接調用它 public static string DecryptStr() 並將其刪除。在您的Main() – DOMZE

+0

@bsteo - 請參閱更新 –