gpt4 book ai didi

File.Copy 中的 C# UnauthorizedAccessException

转载 作者:太空狗 更新时间:2023-10-29 19:55:56 24 4
gpt4 key购买 nike

我正在复习我的 C#,所以我决定编写一个程序,我可以用它轻松导入我拍摄的照片。一点背景……我用 JPEG 和 RAW 拍摄照片,然后浏览并挑选 JPEG,因为它们更小,更容易处理/预览。然后我只导入那些值得在后期制作中处理的 RAW 文件。

我想编写一个简单的程序来从一个目录复制 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:

最佳答案

问题可能是您无法删除或覆盖只读文件。解决方案是更改属性。

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

关于File.Copy 中的 C# UnauthorizedAccessException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18554108/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com