我正在刷新我的C#,因此我決定編寫一個可用於輕鬆導入我拍攝的照片的程序。一點背景......我用JPEG和RAW拍攝照片,然後瀏覽並選擇JPEG,因爲它們更小,更易於處理/預覽。然後我只導入那些在後期製作中值得搞亂的RAW文件。File.Copy中的C#UnauthorizedAccessException
我想編寫一個簡單的程序來從一個目錄中複製RAW文件,該目錄與我在另一個目錄中篩選過的JPEG匹配。
下面是代碼:
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();
}
一切似乎是工作,當我寫的所有變量到控制檯,我期望,但我得到的Copy.File
異常指示文件只讀。我檢查了,但他們不是,但是文件夾本身是,儘管我盡了最大的努力,但我不能將我的測試文件夾作爲只讀文件取消標記。任何幫助將不勝感激,我已經粘貼下面的例外日誌。
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:
Windows資源管理器? –
是的,這是正確的。 – user2736424
您是否檢查過文件夾屬性的安全權限? –