8 Replies - 12252 Views - Last Post: 08 April 2013 - 02:21 PM Rate Topic: -----

#1 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 333
  • Joined: 10-November 09

C# Could Not Find Part of File Path...

Posted 07 April 2013 - 09:52 AM

Hi... I'm getting this error, and I need to know how to overcome it.

So, what I'm trying to do is copy a file to a new location. It bugs out at the bottom of the code.

                        CustomID = GetNewCustomID().ToString();

                        var oldId = Session["OldId"].ToString();
                        var oldPath = @"TempImg/Uploads/" + oldId + "/";
                        var newPath = @"TempImg/Uploads/" + CustomID + "/";

                        if (Directory.Exists(Server.MapPath(oldPath)))
                        {
                            DirectoryInfo directoryInfo = new DirectoryInfo(Server.MapPath(oldPath));
                            Directory.CreateDirectory(Server.MapPath(newPath));

                            FileInfo[] file = directoryInfo.GetFiles("*.*");

                            var oldFile = oldPath + file[0];
                            var newFile = newPath + file[0];

                            if (File.Exists(Server.MapPath(oldFile)))
                            {
                                File.Copy(oldFile, newFile, true);  //THIS IS WHERE IT BUGS OUT
                            }
                        }

Any thoughts?

This post has been edited by mb2000inc: 07 April 2013 - 09:55 AM


Is This A Good Question/Topic? 0
  • +

Replies To: C# Could Not Find Part of File Path...

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,645
  • Joined: 02-June 10

Re: C# Could Not Find Part of File Path...

Posted 07 April 2013 - 11:11 AM

First, building paths like that is horrible and leads to missing '/' or extra '/' all the time. You should look at System.IO.Path.Combine to make your paths.

Next... Look closely at your paths at run-time. Basic debugging should let you see where the path you're providing is wonky. See FAQ 2 below.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Apr 2013
Spoiler



Was This Post Helpful? 1
  • +
  • -

#3 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 333
  • Joined: 10-November 09

Re: C# Could Not Find Part of File Path...

Posted 08 April 2013 - 06:16 AM

Thank you! I will have a look.
Was This Post Helpful? 0
  • +
  • -

#4 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 333
  • Joined: 10-November 09

Re: C# Could Not Find Part of File Path...

Posted 08 April 2013 - 09:56 AM

This did not work. I'm still getting the error.
                        var oldPath = Path.Combine("TempImg/Uploads/", oldId + "/");
                        var newPath = Path.Combine("TempImg/Uploads/", CustomID + "/");

                        if (Directory.Exists(Server.MapPath(oldPath)))
                        {
                            DirectoryInfo directoryInfo = new DirectoryInfo(Server.MapPath(oldPath));
                            Directory.CreateDirectory(Server.MapPath(newPath));

                            FileInfo[] file = directoryInfo.GetFiles("*.*");

                            var oldFile = Path.Combine(oldPath , file[0].ToString());
                            var newFile = Path.Combine(newPath, file[0].ToString());

                            if (File.Exists(Server.MapPath(oldFile)))
                            {
                                File.Copy(oldFile, newFile, true);//still bugging out, here.
                            }



Other thoughts or suggestions?
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,645
  • Joined: 02-June 10

Re: C# Could Not Find Part of File Path...

Posted 08 April 2013 - 10:04 AM

Please take a screen shot showing the IDE stopped on the line throwing the error, and the Locals palette open showing the variables so we can see WHY and WHAT caused it.

If that was all gibberish to you... then go back do do this tutorial as mentioned previously.
http://www.dreaminco...-common-errors/
Was This Post Helpful? 0
  • +
  • -

#6 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 333
  • Joined: 10-November 09

Re: C# Could Not Find Part of File Path...

Posted 08 April 2013 - 10:33 AM

View PosttlhIn`toq, on 08 April 2013 - 12:04 PM, said:

Please take a screen shot showing the IDE stopped on the line throwing the error, and the Locals palette open showing the variables so we can see WHY and WHAT caused it.

If that was all gibberish to you... then go back do do this tutorial as mentioned previously.
http://www.dreaminco...-common-errors/



please see attached image(s)....
Both display correctly...

(edit - I don't think it liked my other attachment - it showed the actual error message.)

trying this one...

Attached image(s)

  • Attached Image

This post has been edited by mb2000inc: 08 April 2013 - 10:32 AM

Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5432
  • View blog
  • Posts: 11,645
  • Joined: 02-June 10

Re: C# Could Not Find Part of File Path...

Posted 08 April 2013 - 11:00 AM

Look at the path...
"TempImg/Uploads/98132810/" is not a valid path. Its only part of a path.

What drive is this supposed to be on? The server... the C: drive...

If you don't understand what a valid file/folder path is, you should become familiar with Windows file system before trying to build an application that uses it.
Was This Post Helpful? 0
  • +
  • -

#8 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 333
  • Joined: 10-November 09

Re: C# Could Not Find Part of File Path...

Posted 08 April 2013 - 11:32 AM

Here's a little back story...
basically, this application is for users to login and create a custom print job. each custom print job is assigned a unique ID (CustomID). They have the ability to upload an image file (logo). The upload code, uses the same "directory structure".

                var path = @"TempImg/Uploads/" + CustomID + "/";

                if (!Directory.Exists(Server.MapPath(path)))
                {
                    Directory.CreateDirectory(Server.MapPath(path));
                }
                //...DO MORE STUFF



This works like a champ - no issues what so ever.

All though, this "TempImg" directory is not actually resting on the local drive. It's a virtual directory in IIS that is pointing to a completely different server (they did it to save space on the IIS server).

Normally, the directory would be C:\[specific client site directory]\CustomPOD\TempImg\Uploads\....etc.

All that being said, would it sill be feasible to use the above directory structure?
Was This Post Helpful? 0
  • +
  • -

#9 mb2000inc  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 333
  • Joined: 10-November 09

Re: C# Could Not Find Part of File Path...

Posted 08 April 2013 - 02:21 PM

ACK! I got it.
Essentially, you were mostly right. BUT!, so was I. Figure that one out, lol.

My Server.MapPath's were in the wrong place(s). Look:
(it works, now - long live the Klingon Empire)
var oldId = Session["OldId"].ToString();
                        var oldPath = Server.MapPath(@"TempImg/Uploads/" + oldId + "/");
                        var newPath = Server.MapPath(@"TempImg/Uploads/" + CustomID + "/");

                        if (Directory.Exists(oldPath))
                        {
                            DirectoryInfo directoryInfo = new DirectoryInfo(oldPath);

                            if (!Directory.Exists(newPath))
                            {
                                Directory.CreateDirectory(newPath);
                            }

                            FileInfo[] files = directoryInfo.GetFiles("*.*");
                            
                            var oldFile = Path.Combine(oldPath , files[0].ToString());
                            var newFile = Path.Combine(newPath, files[0].ToString());

                            if (File.Exists(oldFile))
                            {
                                File.Copy(oldFile, newFile, true);
                            }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1