Why when creating the directory i'm getting error that the given format is not valid?

StackOverflow https://stackoverflow.com/questions/23620530

  •  21-07-2023
  •  | 
  •  

Question

Directory.CreateDirectory(@"c:\temp\newimages\" + "SecondProcess_" +
                    DateTime.Now);

What i want to create is a directory at c:\temp\newimages\ For example:

c:\temp\newimages\SecondProcess_5/13/2014-2:33

NotsupportedException: The given path's format is not supported

System.NotSupportedException was unhandled
  HResult=-2146233067
  Message=The given path's format is not supported.
  Source=mscorlib
  StackTrace:
       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.Directory.InternalCreateDirectoryHelper(String path, Boolean checkHost)
       at System.IO.Directory.CreateDirectory(String path)
       at mws.Animation_Radar_Preview.numericUpDown1_ValueChanged(Object sender, EventArgs e) in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\Animation_Radar_Preview.cs:line 457
       at System.Windows.Forms.NumericUpDown.OnValueChanged(EventArgs e)
       at System.Windows.Forms.NumericUpDown.set_Value(Decimal value)
       at System.Windows.Forms.NumericUpDown.UpButton()
       at System.Windows.Forms.UpDownBase.OnUpDown(Object source, UpDownEventArgs e)
       at System.Windows.Forms.UpDownBase.UpDownButtons.OnUpDown(UpDownEventArgs upevent)
       at System.Windows.Forms.UpDownBase.UpDownButtons.BeginButtonPress(MouseEventArgs e)
       at System.Windows.Forms.UpDownBase.UpDownButtons.OnMouseDown(MouseEventArgs e)
       at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at mws.Program.Main() in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\Program.cs:line 26
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
Was it helpful?

Solution

You have invalid characters in the file name - forward slashes and colons are not allowed.

You'll have to output the date in a format that's allowed, something like the following.

var path = Path.Combine(@"c:\temp\newimages",
    String.Concat("SecondProcess_", DateTime.Now.ToString("MMddyyyy-HHmm")));

And I'd suggest using Path.Combine to safely build the file path, instead of using string concatenation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top