Illegal characters in path

Apr 19, 2009 at 6:59 PM
I'm getting an "Illegal Characters in path" exeption and I can't figure out why.

Here is the code.......
void AddFileToZip(string FileName, string ArchiveName)
    using (ZipFile zip = new ZipFile())

Here is the Stack
System.ArgumentException was unhandled
  Message="Illegal characters in path."
       at System.IO.Path.CheckInvalidPathChars(String path)
       at System.IO.Path.NormalizePathFast(String path, Boolean fullCheck)
       at System.IO.Path.NormalizePath(String path, Boolean fullCheck)
       at System.IO.Path.GetFullPathInternal(String path)
       at System.IO.FileInfo..ctor(String fileName)
       at Ionic.Zip.ZipEntry.FigureCompressionMethodForWriting(Int32 cycle)
       at Ionic.Zip.ZipEntry.WriteHeader(Stream s, Int32 cycle)
       at Ionic.Zip.ZipEntry.Write(Stream outstream)
       at Ionic.Zip.ZipFile.Save()
       at Ionic.Zip.ZipFile.Save(String zipFileName)
       at WindowsFormsApplication1.Form1.AddFileToZip(String FileName, String ArchiveName) in C:\Users\Tony\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 30
       at WindowsFormsApplication1.Form1.cmdStart_Click(Object sender, EventArgs e) in C:\Users\Tony\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 51
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.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(Int32 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 WindowsFormsApplication1.Program.Main() in C:\Users\Tony\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(Assembly 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.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
Apr 20, 2009 at 1:05 PM
Edited Apr 20, 2009 at 1:56 PM
The exception you are getting says, "illegal characters in path".
Sometimes the exception is what it says it is. 

It would help to know what characters are in the path. . . 
Can you use a debugger to verify that the pathname is what you think it should be?

What is the name of the file that is generating this error?
Does it happen with every file?

I can reproduce the stacktrace you reported by using a filename that contains a \t (tab) character.   

EDIT: Also, I suspect your use of the @ prefix is unnecessary, and your use of the @ may be related to the error you are getting.  When prepended to a string literal, the @ tells the compiler to treat the string as verbatim.  In other words, @"dir\test\file.txt" is the same as "dir\\test\\file.txt".    When prepended to an identifier (eg, FileName in your code), the @ prefix tells the compiler to that it is ok to use a keyword as an identifier.  C# keywords include things like "class", "bool" or "struct".  Using this prefix you can do something like this:
  bool @class= false;
  while (! @class)
    @class = DetermineSomeValue();

The @ prefix for identifiers is intended to allow compatibility with other languages. The rule is: If you don't NEED to use a C# keyword as an identifier, don't.   

Your use of the @ prefix is needless.  C# allows you to prefix any identifier with the @ prefix - it need not be a C# keyword.  But if it is not a keyword, then the @ prefix has no effect.  In other words, @FileName is the same as FileName. 

You may be thinking that you want the compiler to treat the contents of FileName as a verbatim string - in other words suppose that FileName contains "c:\temp\test.txt". As you know, \t is the tab character; that string is 14 characters long and has two TABs in it. With your use of the prefix, you may want the compiler to interpret that as "c:\\temp\\test.txt", a string of length 16 with two backslashes in it.  But @FileName will not do this.  If you have single backslashes in the string referred to by FileName, you may need to do a FileName.Replace("\","\\"); 
Apr 20, 2009 at 1:57 PM
Since the error occurred on the this call, zip.Save(""), I didn't even think of check the AddFile parameter which of course was the culprit.

Apr 20, 2009 at 2:23 PM
Ahh, I've been there. Glad you sorted it out.