File not have permission

MiPakTeh 1,476 Reputation points
2023-03-26T08:33:12.84+00:00

What try to do;

During process FilesystemWatcher if that file not have permission then skip and go next.

If have permission then move to "D:\test22".

somebody can show me.



C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,246 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,286 Reputation points Microsoft Vendor
    2023-03-27T07:28:00.7966667+00:00

    @MiPakTeh, Welcome to Microsoft Q&A, based on my test, you could use try catch to ignore the permission error.

    Here is a code example you could refer to.

           private void OnChanged(object source, FileSystemEventArgs e)
            {
                try
                {
                    string path = e.FullPath;
                    if(File.Exists(path))
                    {
                        string filename=Path.GetFileNameWithoutExtension(path);
                        string path1 = Path.Combine(MyPath_2, filename);
                        Console.WriteLine(  path1);
                        Console.WriteLine( "this is file");
                        File.Copy(path, path1, true );
    
                    }
                    if(Directory.Exists(path))
                    {
                        string directory= new DirectoryInfo(path).Name;
                        string path2= Path.Combine(MyPath_2, directory);
                        CopyFilesRecursively(path, path2);
                        Console.WriteLine(  path2);
                        Console.WriteLine("this is directory");
                    }
                }
                catch(Exception ex)
                {
                    Console.WriteLine( ex.Message);
                    
                }
            }
            private void CopyFilesRecursively(string sourceDir, string targetDir)
            {
                //Now Create all of the directories\
                if(!Directory.Exists(targetDir))
                {
                    Directory.CreateDirectory(targetDir);
                }
               
    
                foreach (var file in Directory.GetFiles(sourceDir))
                    File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)),true);
    
                foreach (var directory in Directory.GetDirectories(sourceDir))
                    CopyFilesRecursively(directory, Path.Combine(targetDir, Path.GetFileName(directory)));
            }
    
    

    After using the above code, you could copy the files and directories to the destination folder.

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful