469,330 Members | 1,335 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

Error:Could not find a part of the path

132 100+
Hi there.

I have an ASP.Net form with C# which allows a user to browse to a file and then add the file details to a listbox; I am using a listbox to hold the details for multiple files to enable batch processing.

For each file, I then copy it to a folder called C:\uploads and the file is processed from there.

For the copy command I am using:

File.Copy(listFileName.Text, importFolder);

and it is here that I receive a message:

Could not find a part of the path 'C:\Uploads\'.

Any ideas how to get passed this please, as I have tried a few ideas from the web but none have been applicable so far?

Thank you.

Mark :)
Dec 4 '08 #1
19 18559
Curtis Rutland
3,256 Expert 2GB
This doesn't make any sense to me....

You say you're using ASP.NET? How are you getting any file details without uploading the file? Unless you are serving the app from the same machine that you are browsing from, you shouldn't be able to browse the user's file system.

Please clarify what you're doing.
Dec 4 '08 #2
Plater
7,872 Expert 4TB
The files are on the Clients computer, not on the server computer right?
So when the server executes the SERVER code of File.Copy(), it cannot find these files?
Dec 4 '08 #3
shweta123
692 Expert 512MB
Hi,

To solve the error you are getting , you can do the following things:

1> You should give the filepath as C:\\upload\\
2> You should specify @ chracter for giving special characters.
e.g FilePath = @(c:\upload\)
Dec 4 '08 #4
E11esar
132 100+
Hi again.

I am using an asp:FileUpload to enable browsing to a folder on the local server (currently my laptop) and then adding the details of

FileUpload1.PostedFile.FileName

to a ListBox, as I need to load and process multiple files, so this was a best way idea for me to accommodate this requirement.

I then use a foreach on the listbox details and deposit the listbox text details to a File.Copy() command, so for example

listbox.text = "c:\filename.ext" so this is used in the sourceDirectory part of the Copy command.

I have a constant called importFolder, which is set to "c:\uploads" so the copy is then made up of

File.Copy(sourceDirectory, importFolder);

I think I can see what maybe going amiss here (as I type this) I am not adding in the fileName to the importFolder, so I need to append this to the directory "c:\uploads"..?

Thank you.

Mark :)
Dec 4 '08 #5
E11esar
132 100+
I also have the importFolder defined as

private const string importFolder = @"C:\Uploads\";

Thank you.

M :)
Dec 4 '08 #6
Plater
7,872 Expert 4TB
Ok, so do the users ever actually upload the file to you and you write it out to the directory?

For instance:
Expand|Select|Wrap|Line Numbers
  1. string filename = myFileUpload.FileName;
  2. byte[] filebytes = myFileUpload.FileBytes;    
  3. string path = myLogPath;
  4. string pathandfile = path+@"\"+filename;
  5. try
  6. {
  7.   FileInfo fi = new FileInfo(pathandfile);
  8.   FileStream fs = fi.Open(FileMode.Create, FileAccess.Write);
  9.   fs.Write(filebytes,0,filebytes.Length);
  10.   fs.Flush();
  11.   fs.Close();
  12.   fs.Dispose();
  13. }
  14. catch (Exception ee)
  15. {//todo: log an error??
  16.  
  17. }
  18.  
Dec 4 '08 #7
E11esar
132 100+
Hi again, I have just solved it.

The answer was to append the fileName as mentioned above, hence I used the following code to solve the problem:

foreach (ListItem listFileName in fileList.Items)
{
try
{
.
.
.
string sourcePath = listFileName.Text;
string destPath = Path.Combine(importFolder,Path.GetFileName(listFil eName.Text));

//Copy file, overwriting if necessary
File.Copy(sourcePath, destPath, true);
.
.
.

This then has done the trick.

Thank you for your help.

M :)
Dec 4 '08 #8
Curtis Rutland
3,256 Expert 2GB
Just to save you this headache later....

This isn't going to work once you start testing it from a real webserver rather than your local machine. The server doesn't have any access to your filesystem, so saying File.Copy or whatever will look on the server's file system not the client's. It's working right now because those are the same; you're browsing from your server.

Remember that all .net code is processed and handled on the server.
Dec 4 '08 #9
E11esar
132 100+
Oh that is not good news then..!

Sorry to be a pain but what should I do in this case for when this is running on the server?

Is it best to use the asp.FileUpload and the SaveAs method to save the target file to the uploading directory?

If this is the case then I will be back at square one as I need to process a batch of files and didn't want to add FileUpload's to the web form.

Also to add to this, I am now finding that my current approach is copying across an empty file, whereas the source file is greater than zero bytes..?

Looks like my premature "it is working" was unfounded..!

M :)
Dec 4 '08 #10
Curtis Rutland
3,256 Expert 2GB
Well.....ASP.NET isn't going to be the best way to process multiple files, unless you want to dynamically create multiple FileUpload controls. If it were me...I'd look into using a Silverlight control and a web service. When you "save" files in silverlight, you are actually using local storage on the client's computer. So you can "save" a batch of files locally, and then process them. Write a web service or WCF service that will take a byte[] or maybe a List<byte[]> to do them all at once and do the batch processing, and return a status.

Well, your simple project just got much more complicated :(
Dec 4 '08 #11
Frinavale
9,735 Expert Mod 8TB
@insertAlias
This is one of the reasons why most web sites have restrictions on the number of files you can upload.

@insertAlias
I had no idea that silverlight could do this....
This is a very good excuse to check out silverlight tonight :)
Dec 4 '08 #12
Plater
7,872 Expert 4TB
Think of silverlight as a mix between flash and java applets. That's how I look at it.
Dec 4 '08 #13
Frinavale
9,735 Expert Mod 8TB
@Plater
I don't know about the "Flash" part....
Have you tried using it before?
Dec 4 '08 #14
Curtis Rutland
3,256 Expert 2GB
It's a lot like flash in some ways...it's a rich client side plugin. But you really don't program it the same way...it's not on a timeline.

It is more like an applet than anything, but with much less overhead.
Dec 4 '08 #15
Curtis Rutland
3,256 Expert 2GB
Videos : The Official Microsoft Silverlight Site

This URL might be of interest. It's a sample on how to use silverlight to upload a file through a webservice (i think...i'm pretty sure this is the video I watched that showed that ;))

Now, if this is the video I think it is, i think it uses a REST service, but you could write that as an ASMX or WCF service just as easily, and use a real service reference in your app.
Dec 4 '08 #16
Curtis Rutland
3,256 Expert 2GB
Just to let you know, I'm working on writing a program like the one I described, and I will write an article about it, describing how.

It should be done in a day or so.
Dec 5 '08 #17
E11esar
132 100+
Very interesting - thank you for your help. I guess it is time I bit the bullet and started to study Silverlight.

:)
Dec 7 '08 #18
Curtis Rutland
3,256 Expert 2GB
OK, sorry, things have taken much longer than I wished, but I have finished the project. I will write the article about the project soon. It was an interesting one, that's for sure.

I'll post a link in this thread to the article once it comes up.
Dec 19 '08 #19
Curtis Rutland
3,256 Expert 2GB
OK, here's a Draft of the article. It might move around or change a bit, but you can read it now.
Dec 20 '08 #20

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Christopher Parent | last post: by
reply views Thread by jessica | last post: by
14 posts views Thread by Gianfranco | last post: by
2 posts views Thread by rouqiu | last post: by
1 post views Thread by dotNetDave | last post: by
1 post views Thread by ipramod | last post: by
3 posts views Thread by =?Utf-8?B?SGVyYg==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.