为什么在创建目录时,我得到错误,给定的格式无效

本文关键字:无效 格式 错误 创建目录 为什么 | 更新日期: 2023-09-27 18:03:13

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

我要创建的是一个目录在c:'temp'newimages'例如:

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

NotsupportedException:指定的路径格式不支持

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: 

为什么在创建目录时,我得到错误,给定的格式无效

文件名中有无效字符-不允许使用正斜杠和冒号。

您必须以允许的格式输出日期,如下所示。

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

我建议使用Path.Combine来安全地构建文件路径,而不是使用字符串连接。