“不支持给定路径的格式。”

“The given path's format is not supported.”

我正在尝试使用 iTextSharp 通过 C# 创建 PDF

if (emp.FormDesc == "FormOne")
   {
      string filePath = @"\G:\SharedDrive\Forms\FormOne\" + filename;

       // Write out PDF from memory stream.
       using (FileStream fs = File.Create(filePath))
        {
           fs.Write(content, 0, (int)content.Length);
        }
    }

我正在返回这个字符串

filePath = "\\G:\SharedDrive\Forms\FormOne\11-23-2020.pdf"

但我不断收到错误消息“不支持给定路径的格式。”

有什么想法吗?我的语法似乎是正确的。

谢谢。

改变这个:

string filePath = @"\G:\SharedDrive\Forms\FormOne\" + filename;

为此:

string filePath = @"G:\SharedDrive\Forms\FormOne\" + filename;

或者如果它是网络共享,则如下所示:

string filePath = @"\SharedDrive\Forms\FormOne\" + filename;

其中“SharedDrive”是网络共享的名称。

(根据下面的第一条评论进行编辑)

更好更安全的方法是使用 Path.Combine,如下所示:

 string filePath = Path.Combine("G:\SharedDrive\Forms\FormOne", filename);