Path Format not Supported

J

Jim McGivney

In an aspx page I run the following C# code-behind:
string filename = null;
filename = txtUpload.PostedFile.FileName.Trim();
//save the file
txtUpload.PostedFile.SaveAs(@"C:\home\sites\bobo.com\web\" + filename);

I get the following error: The given path's format is not supported.

Any suggestions as to the proper format for the path would be appreciated.

Thanks,
Jim
 
P

Peter Rilling

Check the variable "filename". Name sure it contains what you expect and
nothing more. Also, see what happens if you remove the ".com" from the
path.
 
G

Guest

Hi Jim,

Looking at your code here my 5 cents :
your code : filename = txtUpload.PostedFile.FileName.Trim(); will return
the full filename
e.g c:\test\fileUploads\filetoupload.txt
hence file name = c:\test\fileUploads\filetoupload.txt

now when you do
txtUpload.PostedFile.SaveAs(@"C:\home\sites\bobo.com\web\" + filename)

you are doing (@"C:\home\sites\bobo.com\web\" +
"c:\test\fileUploads\filetoupload.txt")

hence it will try to save the file in the path :
C:\home\sites\bobo.com\web\c:\test\fileUploads\filetoupload.txt

which is invalid format coz of c:\ being twice and hence you getting the
invalid format error

********************************
Hope this helps,
S.H.A.U.N â„¢ (M.C.P)
Shounak P
http://blogs.wwwcoder.com/shaunakp
*********************************
 
G

Guest

When working with file names and paths it's recommended to use
System.IO.Path-class
For example:
string fName = System.IO.Path.GetFileName(filename) //gets only the file
name from full path
It's also recommended to use System.IO.Path.Combine() method to build paths.
So I would do it like:
txtUpload.PostedFile.SaveAs(Path.Combine(@"C:\home\sites\bobo.com\web",
Path.GetFileName(filename));
 
J

Juan T. Llibre

re:
Yuo should double the backslashes
ex: c:\\inetpub\\.....

Actually, no, because he's using the literal @ :

SaveAs(@"C:\home\sites\bobo.com\web\"

The @ means that he does *not* have to double the backslashes.

The correct answer was posted by Shaunak.

The code concatenates two full file paths,
and that is not acceptable as a path format.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top