Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving file with the same name

Tags:

c#

I have used this to save my file

string m_strDate = DateTime.Now.ToString("MM/dd/yyyy");
m_strDate = m_strDate.Replace("/", "");
strPath += "/FileHeader_" + m_strDate + ".txt";

So as per this I can save a file per day. But if I create for another time the data in that text file is getting replaced by the new one. Now what I need is I would like to save my file with some name along with date and a number like

"/FileHeader_1" + m_strDate + ".txt"

and so on.

like image 295
Developer Avatar asked Aug 30 '26 07:08

Developer


1 Answers

strPath = "/FileHeader_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt";

or check if the file exists:

strPath = "/FileHeader_{0}" + DateTime.Now.ToString("MMddyyyy") + ".txt";
if ( File.Exists( string.format( strPath, "" ) ){
  int i = 1;
  while( File.Exists(string.format( strPath, i ) ){ i++ ; }
  strPath = string.Format(strPath, i);
}
else {
  strPath = string.format( strPath, "" );
}
like image 100
Draco Ater Avatar answered Sep 08 '26 19:09

Draco Ater



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!