利用 FileVersionInfo 類別可輕鬆讀取組件或原生 DLL/EXE 的版本號:
FileVersionInfo info = FileVersionInfo.GetVersionInfo(@"D:\TEST.DLL");
textBox1.Text = $"版本號: {info.FileVersion}";
File.Copy 覆寫時若目標檔案為唯讀會失敗,需先移除唯讀屬性:
string src = @"E:\test.txt";
string dest = @"d:\tmp\test.txt";
if (File.Exists(dest))
{
FileInfo fi = new FileInfo(dest);
if ((fi.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
fi.Attributes = FileAttributes.Normal; // 移除唯讀
File.Delete(dest);
}
File.Copy(src, dest, true);
string str = "1234";
int val = Convert.ToInt32(str); // 或 int.Parse(str)
string s = "1234";
s = s.PadRight(30, ' '); // 尾部補空白至 30 字元
bool fileExists = File.Exists(path);
bool dirExists = Directory.Exists(@"D:\LastestVersion");
int count = 0;
using (StreamReader sr = new StreamReader(textBox1.Text.Trim()))
{
while (sr.Peek() > -1)
{
listBox1.Items.Add(sr.ReadLine());
count++;
}
}
using (StreamWriter sw = new StreamWriter(@"D:\result.txt"))
{
for (int i = 0; i < 10; i++)
sw.WriteLine($"這是第{i}行資料");
}
string file = "";
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.InitialDirectory = @"D:\Patch";
dlg.Filter = "所有檔案 (*.*)|*.*|文字檔 (*.txt)|*.txt";
dlg.FilterIndex = 2;
dlg.RestoreDirectory = true;
if (dlg.ShowDialog() == DialogResult.OK)
file = dlg.FileName;
}
string folder = "";
using (FolderBrowserDialog dlg = new FolderBrowserDialog())
{
dlg.RootFolder = Environment.SpecialFolder.MyComputer;
if (dlg.ShowDialog() == DialogResult.OK)
folder = dlg.SelectedPath;
}