Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Getting error/Exception when i try to copy a file to a Directory with my local path or UNC path.....

Posted on 2009-05-12
8
Medium Priority
?
215 Views
Last Modified: 2012-05-06
Hi

In my application i have function wich Backup imported files to a driectory local or with UNC path to a server ...whatever, but if the path is not correct then i want to get a messagebox and insted of moving the file i wil make copy of that file to the same directory path......

so first i check that the directory exist , if not then the function have to copy the file....but if the path is not valid or wrong path wich doea not exist i get this exception: Could not find a part of the path 'C:........
if ((file.Attributes & FileAttributes.ReadOnly) != FileAttributes.ReadOnly)
                    {
                        DirectoryInfo dirPath = new System.IO.DirectoryInfo(toPath);
 
                        if (dirPath.Exists == true)
                        {
                            file.MoveTo(toPath);
                        }
                        else
                        {
                            MessageBox.Show(GetValue("couldntMoveFile") + fileName + GetValue("madeacopyinstead"), GetValue("warning"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            file.CopyTo(toPath);
                            return;
                        }                        
                    }

Open in new window

0
Comment
Question by:Adnan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 3

Expert Comment

by:Aleksei_Malkov
ID: 24372090
use Directory.Exists method instead of DirectoryInfo
0
 
LVL 13

Expert Comment

by:drypz
ID: 24372264
Hi! Maybe the problem is the value of your variable "toPath";

set it like this...

string toPath = @"C:\SubDirectory";
0
 

Author Comment

by:Adnan
ID: 24372562
the value of the path is the backupfolder og UNC path to a directory set by user in application as settings.....
and it is declared like this:  string toPath = myFileTypeSettings.BackupPath;

i was in a meeting so i wil try to set Directory.Exists method instead of DirectoryInfo to se if it works...?!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Adnan
ID: 24372570
here is the full method/function:
   private void BackupImportedFiles()
        {
 
            if (myFileTypeSettings.BackupPath.Trim() != "" && toolStripMenuItemMoveFilesToBackup.Checked)
            {
                foreach (SimpleFile objFile in arrayImportFiles)
                {
                    String fileName = objFile.FileName;
                    string toPath = myFileTypeSettings.BackupPath;
                    DateTime now = System.DateTime.Now;
                    string date = now.ToString();
                    int startpos = fileName.LastIndexOf("\\") + 1;
                    int lengde = toPath.Trim().Length;
                    // Adds \ at the end of the backup path it not exists)
                    if (toPath.Substring(lengde) != "\\")
                    {
                        toPath += "\\";
                    }
 
                    date = date.Replace(" ", "_");
                    date = date.Replace(System.Globalization.DateTimeFormatInfo.CurrentInfo.TimeSeparator, "");
                    date = date.Replace(System.Globalization.DateTimeFormatInfo.CurrentInfo.DateSeparator, "");
                    toPath += date + " " + fileName.Substring(startpos);
 
                    FileInfo file = new FileInfo(fileName);
 
 
                    if ((file.Attributes & FileAttributes.ReadOnly) != FileAttributes.ReadOnly)
                    {
                        DirectoryInfo dirPath = new System.IO.DirectoryInfo(toPath);
 
                        if (dirPath.Exists == true)
                        {
                            file.MoveTo(toPath);
                        }
                        else
                        {
                            MessageBox.Show(GetValue("couldntMoveFile") + fileName + GetValue("madeacopyinstead"), GetValue("warning"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            file.CopyTo(toPath);
                            return;
                        }                        
                    }
                }
            }
        }

Open in new window

0
 
LVL 13

Expert Comment

by:drypz
ID: 24372964
Hi! Something like this...

file.MoveTo(toPath +  "filename");
0
 

Author Comment

by:Adnan
ID: 24373139
hmm no its not problom with moving the file or copy it to the directory path, every thing works fine until i define a wrong path f.eks:

Correct path: C:\AmA\AmABackup\backuptest
Every thing works fine until i do something wrong with the path.....


wrong path: C:\AmA\AmABackup\backuptest123

and the same problom with UNS phats...!!!

I just want my function/application to check the path if it is valid/correct path to the directory or it is a error on the path
0
 

Author Comment

by:Adnan
ID: 24373167
the problom is that if the function cant move file to the path, then it has to make a copy of that file into the directory/path, but the path is not correct and i get exception saying Could not find a part of the path 'C: because there are no correct path to copy file to....
0
 

Accepted Solution

by:
Adnan earned 0 total points
ID: 24382169
in my case this is the correct solution.....
 try
                    {
                        if ((file.Attributes & FileAttributes.ReadOnly) != FileAttributes.ReadOnly)
                        {
                            file.MoveTo(toPath);
                        }
                        else
                        {
                            MessageBox.Show(GetValue("couldntMoveFile") + fileName + GetValue("madeacopyinstead"), GetValue("warning"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            file.CopyTo(toPath);
                        }
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show(GetValue("couldtnfindthepath"), GetValue("warning"), MessageBoxButtons.OK, MessageBoxIcon.Warning);    
                    }

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question