开发者

how can i format the file name like it includes date and time and month and year

At the moment I am using to store the zip files with the file name like this...

backup-20111010092345.Zip

but i want to change the file name to this ..backup-2011-10-10_09:23:45.Zip

i have got this cod开发者_JAVA技巧e ...

string zipName = Path.Combine(filepath, string.Format("backup-{0}.zip", DateTime.Now.ToString("yyyyMMddhhmmss")));
string backupFilePath = Path.Combine(filepath, backupName);
  using (ZipFile zip = new ZipFile())
  {
    zip.AddFile(backupFilePath, "");
    zip.Save(zipName);

  }



string backupName = "backup.sql";
 string filepath = @"C:\Folder\Back\";

would any one pls help on this... many thanks In advance...

Modified Code:

  string zipName = Path.Combine(filepath, string.Format("backup-{0:yyyy-MM-dd_HH:mm:ss}.zip", DateTime.Now));
  string backupFilePath = Path.Combine(filepath, backupName);
  using (ZipFile zip = new ZipFile())
  {
    zip.AddFile(backupFilePath, "");
    zip.Save(zipName);

  }

Error :Notsupported Exception was unhandled

this is stack trace .

     at System.Security.Util.StringExpressionSet.CanonicalizePath(String path, Boolean needFullPath)
   at System.Security.Util.StringExpressionSet.CreateListFromExpressions(String[] str, Boolean needFullPath)
   at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
   at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String[] pathList, Boolean checkForDuplicates, Boolean needFullPath)
   at System.IO.File.Move(String sourceFileName, String destFileName)
   at Ionic.Zip.ZipFile.Save()
   at Ionic.Zip.ZipFile.Save(String fileName)

error: The given path's format is not supported.


Sounds like you've nearly got it (in terms of building the name that you specified) - you just need to change the format string

string zipName = Path.Combine(filepath,
    string.Format("backup-{0}.zip",
                  DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss"));

You could specify that as:

string zipName = Path.Combine(filepath,
    string.Format("backup-{0:yyyy-MM-dd_HH:mm:ss}.zip",
                  DateTime.Now));

It's up to you which you find more readable.

Note that this will use the time separator for the current culture. If you always want it to be "colon" then you should quote it. On the other hand, is colon even a valid character in Windows filenames? Consider using dash again, or something similar. For example:

string zipName = Path.Combine(filepath,
    string.Format("backup-{0:yyyy-MM-dd_HH-mm-ss}.zip",
                  DateTime.Now));


You'll need to use something other than : as it is reserved. I suggest something like:

 DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss");


As well as the build in formats, you can get the individual components of a DateTime object using its properties like myDate.Year etc. These are detailed on MSDN here:

http://msdn.microsoft.com/en-us/library/991wfdee(v=VS.90).aspx

So if you wanted some really odd formatting you could put together a composite string from each component part in whatever pattern you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜