Tag Archives: .Net Troubleshooting

CS1617: Invalid option ‘6’ for /langversion; must be ISO-1, ISO-2, 3, 4, 5 or Default

在公司Download / 執行網上教學的Sample Code 來測試時
久不久便會出現以下的錯
CS1617: Invalid option ‘6’ for /langversion; must be ISO-1, ISO-2, 3, 4, 5 or Default
CS1617: Invalid option '6' for /langversion; must be ISO-1, ISO-2, 3, 4, 5 or Default

這是因為Sample Code是用了Visual Studio 2015 來建立的 Default的 .Net Framework4.5.2 / 4.6
而公司是使用Visual Studio 2013.Net Framework是 4.5

所以Web.Config上有小小Mis-match
解決方便 十分簡單
我們只需要更改 compilerOptions=”/langversion:6 改變成 compilerOptions=”/langversion:5 便可以了

更改前

<system.codedom>
 <compilers>
 <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider,
 System, Version=4.0.0.0, Culture=neutral,
 PublicKeyToken=b77a5c561934e089" warningLevel="4" 
 compilerOptions="/langversion:6 /nowarn:1659;1699;1701">
 <providerOption name="CompilerVersion" value="v4.0"/>
 </compiler>
 <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
 </compilers>
 </system.codedom>

Original Web.config
更新後

 <system.codedom>
 <compilers>
 <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CSharp.CSharpCodeProvider,
 System, Version=4.0.0.0, Culture=neutral,
 PublicKeyToken=b77a5c561934e089" warningLevel="4" 
 compilerOptions="/langversion:5 /nowarn:1659;1699;1701">
 <providerOption name="CompilerVersion" value="v4.0"/>
 </compiler>
 <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
 </compilers>
 </system.codedom>

Web.config for VS2013
CS1617 Issue Fixed
Hope you find it useful

.Net How to Add slash correctly for Path – 如何安全地建立資料夾路徑

公司需要寫了一個小小的Document Repository 的功能

還記得之前自己經驗有時候使用者會把他們輸入的路徑會有”/“.. 但是有時卻沒有
所以我之前便寫了一個很無聊的功能 AddSlash.. 去檢查 User Input是不是以 / 來開首或結尾..
之後再幫這個User Input 適當地加上 Slash

最近發現原來.Net Framework自己已經有一個很好用的功能
Path.combine. 你可以把Path 放進他的變數內..
之後他便會幫你建立你的Path/路徑

E.G.

using System;
					
public class Program
{
	public static void Main()
	{
		string driveLetter = @"D:\";
		string folder1 = "Data";
		string folder2 = @"Files\";
	    string folder3 = @"sharechiwai";
		string t = System.IO.Path.Combine(driveLetter, folder1, folder2,folder3);
		
		// Return output
		Console.WriteLine(t);
	}
}

.Net Path.Combine functionHope you find it useful

.Net Wildcard Pattern Matching – 在.Net上如果使用Wildcard的Pattern matching呢?

現在寫程式時..很多人會使用Regular Expression 來做Pattern Matching
來檢查字串的值符合 pattern 所包含的模式嗎.
http://msdn.microsoft.com/zh-tw/library/swf8kaxw.aspx

VB.Net上 他有一個 LIKEOperator/運算子
可以做到這個動作

可惜如果你是使用C Sharp的話
你便要自己寫一些功能去把這些Wildcard 轉換成Regular ExpressionPattern

以下是我寫的功能

        /// This static function is used to replace the regular wild card search text pattern to Regular Express pattern
        public static string WildCardToRegexPattern(string pattern)
        {
            return "^" + Regex.Escape(pattern).Replace("\\*", ".*").Replace("\\?", ".") + "$";
        }     

        /// This function is used to execute the wild card match which is similar to VB's LIKE function
        public static bool WildCardMatch(string input, string pattern)
        {
            // Execute pattern matching against the wild card after convert the wild card to Regular Expression pattern via WildCardToRegexPattern function
            return Regex.IsMatch(input, WildCardToRegexPattern(pattern));
        }

使用方法:

string input = "abc111";
string pattern = "*11*";

Console.WriteLine(WildCardMatch(input, pattern));

Hope you find it useful

Exceeded retry count of 10. Failed – Could not copy dll to bin\debug

最近在開發的一個WinForm的程式時常常出現以下的錯誤信息
Could not Copy. Exceeded retry count of 10. Failed.
Could not copy d:\tfsservice\sharechiwai\bin\debug\sharechiwai.dll’ to “bin\Debug\sharechiwai.dll”. Exceeded retry count of 10. Failed.

解決方法:
大家可以到 工具欄上 按”Build” -> “Clean Solution
之後再按 “Build” -> “Rebuild Solution
Build -> Clean Solution

之後應該可以Debug 這個Project的

Hope you find it useful

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

今天嘗試便用 JavaScriptSerializer 把公司一個資料庫的資料轉成 Json 放在公司的另一個程式上做Performance Test的時候出現以下的錯誤信

An unhandled exception of type ‘System.InvalidOperationException’ occurred in System.Web.Extensions.dll

Additional information: Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.
Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

原因是因為我有太多資料..而預設的Json 字串大小是2097152個字元… 我的數據..文字比較多..
便所以出現這個錯誤信息..
解決方法 十分簡單..只是把 SerializerMaxJsonLength 的值比你的 資料更大便可以了

解決方法:

E.G.

// 建立 JavascriptSerializer Object
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
// 查看 JavascriptSerializer Object的 Default MaxJsonLength 是什麼
Console.WriteLine("Default MaxJsonLength value: " + serializer.MaxJsonLength.ToString());
// 更新 JavascriptSerializer Object的 MaxJsonLength 大小... 為Integer的最大值..因為他是Int32的 DataType
serializer.MaxJsonLength = Int32.MaxValue;
Console.WriteLine("Updated MaxJsonLength value: " + serializer.MaxJsonLength.ToString());

這應該可以解決這個問題

Hope you find it useful