Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 871369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:37:52+00:00 2026-05-15T10:37:52+00:00

I’m working on updating a project which uses the following code to place a

  • 0

I’m working on updating a project which uses the following code to place a file in a folder based on the date:

int month = DateTime.Now.Month;
string zero = (month < 10) ? "-0" : "-";
string folder = "Folder" + DateTime.Now.Year.ToString() + zero + month.ToString() + "\\";
if (!Directory.Exists(folder))
{
   Directory.CreateDirectory(folder);
}
ArchiveFolder = folder;
//...
DateTime now = DateTime.Now;
int date = now.Day;
string zero = (date < 10) ? "0" : "";
string saveaspath =  zero + date.ToString() + "_" + now.ToLongTimeString() + "_" + name;
#if DOTNET20
foreach (char c in Path.GetInvalidFileNameChars())
#else
foreach (char c in Path.InvalidPathChars)
#endif
   saveaspath = saveaspath.Replace(c, '-'); // substitute - for bad chars
saveaspath = saveaspath.Replace(':', '-');
string originalname = saveaspath;
string ext = ".ext";
saveaspath = Path.Combine(ArchiveFolder, saveaspath + ext);
int count = 1;
while (File.Exists(saveaspath)) // make unique
{
    string num = " (" + count++.ToString() + ")";
    saveaspath = Path.Combine(ArchiveFolder, originalname + num + ext);
}

Example generated file:
Folder/2010-06/18_2-42-09 PM_name.ext

This is run for each file to be created, usually many files at a time due to the nature of the program.

I was thinking about updating the concatenated DateTimes with DateTime format strings–I figured that might be more efficient than all these different ToString() etc. calls; however I’m nervous that might screw up when a user has different culture settings. Then again, this current code might do something funky–I don’t know.

So: Should I redo the code using format strings? Or leave it as it is? Would it benefit performance? Would it be culture-dependent?

(To clarify: my question is not HOW to rewrite it using a format string; rather, I am concerned with the ramifications of doing so.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:37:52+00:00Added an answer on May 15, 2026 at 10:37 am

    Should I redo the code using format strings?

    It would be a lot more robust and maintainable if it were simplified to use format strings.

    Or leave it as it is?

    If not breaking the existing behaviour is very important to you then it might be best to leave it. On the other hand, if it turns out that there is a bug in the existing code then it might be a good oppurtunity to rewrite it from scratch.

    Would it benefit performance?

    If your code is writing to files I hardly think the time spent creating the strings will be the bottleneck. Writing to a file is a very slow operation in comparison.

    Would it be culture-dependent?

    Use CultureInfo.InvariantCulture when formatting to avoid culture dependence.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.