1

I have created event receiver (Item added) which is attached to document library. Whenever document is added/uploaded in document library which should create copy of it on file system.Following is my code.

  public override void ItemAdded(SPItemEventProperties properties)
    {
        try
        {
            SPSecurity.RunWithElevatedPrivileges(delegate ()
            {
                using (SPSite site = properties.OpenSite())
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPFile file = properties.ListItem.File;
                        FileStream stream = new FileStream("‪E:\\DownloadedImages\\" + file.Name, FileMode.Create);
                        //Error on above line
                        BinaryWriter writer = new BinaryWriter(stream);
                        writer.Write(file.OpenBinary());
                        writer.Close();
                    }
                }
            });
        }
        catch (Exception ex)
        {
        }
        base.ItemAdded(properties);
    }

I am getting following error on mentioned line.

An exception of type 'System.NotSupportedException' occurred in mscorlib.dll but was not handled in user code Additional information: The given path's format is not supported.

1

As Ali mentioned, we can use Path.Combine.

Secondly, you dont need to use BinaryWriter, you can use the FileStream's write method itself.

Try with this below code:

SPFile file = properties.ListItem.File;
var byteArray = file.OpenBinary();
string filePath = Path.Combine("‪E:\\DownloadedImages", file.Name);

using(FileStream fs = new FileStream(filePath, FileMode.Create)
{
  fs.Write(byteArray, 0, byteArray.Length);
}
1

Two things:

Either do as Akshay suggests and put in @ before the path or use Path.combine

https://msdn.microsoft.com/en-us/library/system.io.path.combine.aspx

The second issue is if the above doesn't work. Put a break point on the code below or pump out to a text file the file.Name:

FileStream stream = new FileStream("‪E:\\DownloadedImages\\" + file.Name, FileMode.Create);

The reason why I ask is because it could be that you have illegal characters being used in the file name. Make a function or class to handle it:

https://stackoverflow.com/questions/2435894/net-how-do-i-check-for-illegal-characters-in-a-path

EDIT

Me being an idiot I forgot to mention, no need for double \\ it would break as you are using @ which would explain the error on both ends "The given path's format is not supported", below would work!.

FileStream stream = new FileStream(@"‪E:\DownloadedImages\" + file.Name, FileMode.Create);
  • tried but no luck.File name is msg.txt – Akshay Belure Feb 14 '18 at 8:55
  • just updated my answer, looks like a simple mistake that i left out. – Ali Jafer Feb 15 '18 at 8:28
0

Try to use @"E:\" to solve your problem try this

0

Answer of following question solved my issue.

https://stackoverflow.com/questions/24856821/what-is-causing-notsupportedexception-the-given-paths-format-is-not-supported

This is because, : character is at third position in the string.This should be at second position.I was coping the path from windows explorer and while coping it adds the hidden character and push the :(colon) character to 3rd position.When I typed it manually,It works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.