So I have here a working code in my local machine.
What does is do is it will rename today's textfile.
Path
E:\\Rename
Textfiles:
WithdrawalConfirm20200305.txt
paymentpagecalls20200223.txt
Output:
WithdrawalConfirm20200305.1400.txt
paymentpagecalls20200223.1400.txt
where .XXXX is the time you run the program.
public Form1()
{
InitializeComponent();
}
string dateandtimevar = System.DateTime.Now.ToString("HHmm");
private void Form1_Load(object sender, EventArgs e)
{
string FolderPath = @"E:\Rename";
DirectoryInfo di = new DirectoryInfo(FolderPath);
var files = di.EnumerateFiles($"*{DateTime.Now.ToString("yyyyMMdd")}.txt")
.ToList();
foreach (var file in files)
{
//Parsing files
var newFile = file.FullName.Substring(0, file.FullName.Length - 3);
// Append new date
newFile = newFile + DateTime.Now.ToString("HHmm") + ".txt";
//Rename
File.Move(file.FullName, newFile);
Now my problem is when deploying to win 2008 R2 server
it doesn't show any errors but it won't rename the text file like it does on my local machine. What could be wrong ?
I already checked the file path and the file exist on the server.
You should check the file extension of the file and make sure if it is .txt
if not change it in your desired extension.
For example:
var files = di.EnumerateFiles($"*{DateTime.Now.ToString("yyyyMMdd")}.log")
newFile = newFile + DateTime.Now.ToString("HHmm") + ".log";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.