這是非常可行的。下面是我颳起了你工作示例:編碼
using System;
// The contents of your file
var contents = "19.1.354.6";
// A List<string> of the differnt version types
var pieces = contents.Split('.');
// Variables to hold the versions separatly
var majorVersion = pieces[0];
var minorVersion = pieces[1];
var buildVersion = pieces[2];
var revisVersion = pieces[3];
// Write values to the console (not needed)
Console.WriteLine("Major Version: {0}", majorVersion);
Console.WriteLine("Minor Version: {0}", minorVersion);
Console.WriteLine("Build Version: {0}", buildVersion);
Console.WriteLine("Revision Version: {0}", revisVersion);
// Outputs:
// Major Version: 19
// Minor Version: 1
// Build Version: 354
// Revision Version: 6
快樂:)
編輯:這很可能是你的「的.cs'文件的內容是不是你所期望的是什麼。你認爲(或者更確切地說,希望)它是「19.1.354.6」,但它很可能是其他的東西。你可以很容易地檢查,看它是否真的是你認爲它是寫的內容輸出到控制檯:
// Read contents of file
var contents = System.IO.File.ReadAllText(@"C:\Applicaion\version.cs");
// Write contents to console
Console.WriteLine(content);
你遇到什麼問題?你的意思是不工作?有什麼異常? – User2012384
如果我用'「19.1.354.6」'替換'System.IO.File.ReadAllText(@「C:\ Applicaion \ version.cs」)'',那麼你的代碼工作正常。該文件必須與「19.1.354.6」'不同。 – Enigmativity
具有確切文本「19.1.354.6」的文件不是有效的C#程序,所以我懷疑該文件中有更多。 –