C# 當下時間字串當檔案儲存檔名
C# 當下時間字串當檔案儲存檔名
Code:
//當下時間字串: DateTime.Now.ToString("yyyyMMddHHmmss"); //C# 儲存檔案對話盒 SaveFileDialog //C# 寫入文字檔StreamWriter String StrPath = ""; SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "SYCSV File|*.symap"; saveFileDialog1.Title = "Save an SYMap"; saveFileDialog1.FileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".symap"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { StrPath = saveFileDialog1.FileName.ToString(); StreamWriter sw = new StreamWriter(StrPath, false, System.Text.Encoding.UTF8); sw.WriteLine(StrData); sw.Close(); MessageBox.Show("Save complete.", saveFileDialog1.FileName, MessageBoxButtons.OK, MessageBoxIcon.Information); }
4 thoughts on “C# 當下時間字串當檔案儲存檔名”
/*獲取今天日期(詳細時間-毫秒)*/
//設定DateTime初始值
DateTime GetNowDateTimeDetail = new DateTime(0001, 01, 01, 01, 01, 01, 01);
//獲取今天日期
GetNowDateTimeDetail = DateTime.Now;
//放入string並設定格式
string strTime = DateTimeMillisecond.ToString("yyyy-MM-dd hh:mm:ss fff");
using System.Globalization;
using System.Text.RegularExpressions;
string dateString = "7/16/2008 8:32:45.126 AM";
try
{
DateTime dateValue = DateTime.Parse(dateString);
DateTimeOffset dateOffsetValue = DateTimeOffset.Parse(dateString);
// Display Millisecond component alone.
Console.WriteLine("Millisecond component only: {0}",
dateValue.ToString("fff"));
Console.WriteLine("Millisecond component only: {0}",
dateOffsetValue.ToString("fff"));
// Display Millisecond component with full date and time.
Console.WriteLine("Date and Time with Milliseconds: {0}",
dateValue.ToString("MM/dd/yyyy hh:mm:ss.fff tt"));
Console.WriteLine("Date and Time with Milliseconds: {0}",
dateOffsetValue.ToString("MM/dd/yyyy hh:mm:ss.fff tt"));
string fullPattern = DateTimeFormatInfo.CurrentInfo.FullDateTimePattern;
// Create a format similar to .fff but based on the current culture.
string millisecondFormat = $"{NumberFormatInfo.CurrentInfo.NumberDecimalSeparator}fff";
// Append millisecond pattern to current culture's full date time pattern.
fullPattern = Regex.Replace(fullPattern, "(:ss|:s)", $"$1{millisecondFormat}");
// Display Millisecond component with modified full date and time pattern.
Console.WriteLine("Modified full date time pattern: {0}",
dateValue.ToString(fullPattern));
Console.WriteLine("Modified full date time pattern: {0}",
dateOffsetValue.ToString(fullPattern));
}
catch (FormatException)
{
Console.WriteLine("Unable to convert {0} to a date.", dateString);
}
// The example displays the following output if the current culture is en-US:
// Millisecond component only: 126
// Millisecond component only: 126
// Date and Time with Milliseconds: 07/16/2008 08:32:45.126 AM
// Date and Time with Milliseconds: 07/16/2008 08:32:45.126 AM
// Modified full date time pattern: Wednesday, July 16, 2008 8:32:45.126 AM
// Modified full date time pattern: Wednesday, July 16, 2008 8:32:45.126 AM
C# 時間 取 毫秒(ms ) ~ fff
C# 寫檔 檔案 對話盒
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = “SYCSV File|*.symap”;
saveFileDialog1.Title = “Save an SYMap”;
C# 讀檔 檔案 對話盒
OpenFileDialog OpenFileDialogBuf = new OpenFileDialog();
OpenFileDialogBuf.Filter = “Files|*.bmp;*.jpg;*.jpeg;*.png”;
OpenFileDialogBuf.Title = “Open an Image File”;
OpenFileDialogBuf.ShowDialog();