2013-09-01 9 views
6

Oczyszczam moje C#, więc postanowiłem napisać program, którego mogę użyć do łatwego importowania zdjęć, które wykonuję. Małe tło ... Fotografuję w formacie JPEG i RAW, a następnie przeglądam pliki JPEG, ponieważ są one mniejsze i łatwiejsze w obsłudze/podglądu. Następnie importuję tylko te pliki RAW, które warto zmylić w postprodukcji.Wyjątek C# UnauthorizedAccessException w pliku File.Copy

Chciałem napisać prosty program do kopiowania plików RAW z jednego katalogu, który pasuje do plików JPEG, które przerzuciłem w innym.

Oto kod:

static void Main(string[] args) 
    { 
     Console.WriteLine("Enter the JPEG Origin Directory: "); 
     string originDirectory = @"C:\Users\Greg\Pictures\Summer 2013\Back Bay\testJPEG"; 

     Console.WriteLine("Enter the RAW Origin Directory: "); 
     string copyDirectory = @"C:\Users\Greg\Pictures\Summer 2013\Back Bay\testRAW"; 

     Console.WriteLine("Enter the RAW Import Directory: "); 
     string rawCopyDirectory = @"C:\Users\Greg\Pictures\Summer 2013\Back Bay\testRAWImport"; 

     char[] delimiterChars = { '_', '.' }; 

     List<string> filesToCopy = new List<string>(); 
     List<string> CopiedFiles = new List<string>(); 

     foreach (var filePath in Directory.GetFiles(originDirectory)) 
     { 
      Console.WriteLine("Filepath: '{0}'", filePath); 
      string[] words = filePath.Split(delimiterChars); 

      filesToCopy.Add(words[1]); 
     } 

     filesToCopy.ForEach(Console.WriteLine); 

     foreach (var copyFilePath in Directory.GetFiles(copyDirectory)) 
     { 
      string[] delimited = copyFilePath.Split(delimiterChars);  

      if (filesToCopy.Contains(delimited[1])) 
      { 
       Console.WriteLine("Copied: '{0}'", copyFilePath); 

       string fileName = Path.GetFileName(copyFilePath); 

       string sourcePath = Path.GetDirectoryName(copyFilePath); 

       string targetPath = rawCopyDirectory; 

       string sourceFile = System.IO.Path.Combine(sourcePath, fileName); 

       string destFile = System.IO.Path.Combine(targetPath, fileName); 


      System.IO.File.Copy(sourcePath, destFile, true); 

      } 


     } 

     Console.WriteLine("Press any key to exit."); 
     Console.ReadKey(); 

    } 

Wszystko wydaje się działać jak będę oczekiwać kiedy piszę wszystkie zmienne do konsoli, jednak dostaję wyjątek na Copy.File która wskazuje pliki są tylko czytać. Sprawdziłem, a nie są, jednak sam folder jest, i pomimo moich najlepszych starań nie mogę odeprzeć folderów testowych jako readonly. Każda pomoc będzie doceniona, wkleiłem poniższy dziennik wyjątków.

System.UnauthorizedAccessException was unhandled 
    HResult=-2147024891 
    Message=Access to the path 'C:\Users\Greg\Pictures\Summer 2013\Back Bay\testRAW' is denied. 
    Source=mscorlib 
    StackTrace: 
     at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 
     at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost) 
     at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite) 
     at ConsoleApplication1.Program.Main(String[] args) in C:\Users\Greg\documents\visual studio 2010\Projects\Photo Importer\Photo Importer\photoImporter.cs:line 56 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel) 
     at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly() 
     at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData) 
     at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext) 
     at System.Activator.CreateInstance(ActivationContext activationContext) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 
+0

Eksplorator Windows? –

+0

Tak, zgadza się. – user2736424

+0

Czy sprawdziłeś uprawnienia zabezpieczeń we właściwościach folderu? –

Odpowiedz

10

Problem może polegać na tym, że nie można usuwać ani zastępować plików tylko do odczytu. Rozwiązaniem jest zmiana atrybutów.

if(File.Exists(destFile)) 
{ 
    File.SetAttributes(destFile, FileAttributes.Normal); 
} 
File.Copy(sourcePath, destFile, true); 
2

Okazuje się, że dzwoni niewłaściwy zmienną File.Copy, a zamiast tego próbował skopiować ścieżkę zamiast pliku (Derp). Wszystko działa teraz! Dzięki za odpowiedzi!

Powiązane problemy