在File.Copy C#UnauthorizedAccessException [英] C# UnauthorizedAccessException in File.Copy
问题描述
我在我的C#刷牙,所以我决定写一个程序,我可以用它来轻松地导入我拍照。一点背景...我在拍摄JPEG和RAW照片,然后再通过并选择通过JPEG文件,因为它们是更小,更容易处理/预览。然后,我只导入是值得在后期制作那些搞乱RAW文件。
我想写一个简单的程序复制从一个目录RAW文件相匹配,我已经在另一个通过筛选的JPEG格式的。
下面是代码:
静态无效的主要(字符串[ ]参数)
{
Console.WriteLine(请输入JPEG产地目录:);
串originDirectory = @C:\Users\Greg\Pictures\Summer 2013\Back Bay\testJPEG
Console.WriteLine(请输入原始产地目录:);
串copyDirectory = @C:\Users\Greg\Pictures\Summer 2013\Back Bay\testRAW
Console.WriteLine(请输入原始导入目录:);
串rawCopyDirectory = @C:\Users\Greg\Pictures\Summer 2013\Back Bay\testRAWImport
的char [] delimiterChars = {'_','。'};
名单,LT;字符串> filesToCopy =新的List<串GT;();
名单,LT;字符串> CopiedFiles =新的List<串GT;();
的foreach(在Directory.GetFiles VAR文件路径(originDirectory))
{
Console.WriteLine(文件路径:{0}',文件路径);
的String []字= filePath.Split(delimiterChars);
filesToCopy.Add(字[1]);
}
filesToCopy.ForEach(Console.WriteLine);
的foreach(在Directory.GetFiles VAR copyFilePath(copyDirectory))
{
的String []分隔= copyFilePath.Split(delimiterChars);
如果(filesToCopy.Contains(分隔[1]))
{
Console.WriteLine(复制:{0}',copyFilePath);
字符串文件名= Path.GetFileName(copyFilePath);
串SOURCEPATH = Path.GetDirectoryName(copyFilePath);
串TARGETPATH = rawCopyDirectory;
串的SourceFile = System.IO.Path.Combine(SOURCEPATH,文件名);
串destFile = System.IO.Path.Combine(TARGETPATH,文件名);
System.IO.File.Copy(SOURCEPATH,destFile,真正的);
}
}
Console.WriteLine(按任意键退出。);
Console.ReadKey();
}
一切似乎是工作,我期望我的时候所有的变量写入到控制台,但是我得到的 Copy.File
的异常,表示文件是只读的。我查了一下,他们都没有,但是该文件夹本身,尽管我尽了最大努力,我不能取消标记我的测试文件夹为只读。任何帮助,将不胜感激,我已经粘贴下面的异常日志。
System.UnauthorizedAccessException的是未处理的
=的HResult -2147024891
消息=的路径C:\Users\Greg\Pictures\Summer 2013\Back Bay\testRAW访问被拒绝。
来源= mscorlib程序
堆栈跟踪:
在System.IO .__ Error.WinIOError(的Int32的errorCode,字符串maybeFullPath)
在System.IO.File.InternalCopy(字符串sourceFileName,字符串destFileName布尔覆盖,布尔checkHost)
在System.IO.File.Copy(字符串sourceFileName,字符串destFileName,布尔覆盖)
在ConsoleApplication1.Program.Main(字串[] args)在C:\ Users\Greg\documents\visual工作室2010\Projects\Photo Importer\Photo Importer\photoImporter.cs:在System.AppDomain._nExecuteAssembly线56
(RuntimeAssembly组装,字串[] args)在System.AppDomain.nExecuteAssembly
(RuntimeAssembly组装,字串[] args)在System.Runtime.Hosting.ManifestRunner.Run
(布尔checkAptModel)
在System.Runtime.Hosting.ManifestRunner。 ExecuteAsAssembly()在System.Runtime.Hosting.ApplicationActivator.CreateInstance(activationContext activationContext,字符串[] activationCustomData)在System.Runtime.Hosting.ApplicationActivator.CreateInstance
(activationContext activationContext)
。在系统
.Activator.CreateInstance(ActivationContext activationContext)
在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
在System.Threading.ExecutionContext .RunInternal(ExecutionContext中的ExecutionContext,ContextCallback回调,对象状态,布尔preserveSyncCtx)
在System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象状态,布尔preserveSyncCtx)
在System.Threading.ExecutionContext .RUN(ExecutionContext中的ExecutionContext,ContextCallback回调,对象状态)
在System.Threading.ThreadHelper.ThreadStart()
的InnerException:
您试图访问你的程序可以使用什么样之外的文件。
尝试寻找这个较早的帖子 System.UnauthorizedAccessException:对被拒绝的路径一>
I am brushing up on my C# so I decided to write a program that I can use to easily import photos that I take. A little background...I shoot photos in JPEG and RAW and then go through and pick through the JPEGs since they are smaller and easier to handle/preview. I then import only those RAW files that are worth messing with in post production.
I wanted to write a simple program to copy the RAW files from one directory that match the JPEGs that I've sifted through in another.
Here is the code:
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();
}
Everything seems to be working as I'd expect when I write all the variables to the console, however I'm getting an exception on Copy.File
that indicates the files are read only. I checked, and they aren't, however the folder itself is, and despite my best efforts I cannot unflag my test folders as readonly. Any help would be appreciated, I've pasted the exception log below.
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:
You are trying to access a file outside of what your program can use.
Try looking at this older post System.UnauthorizedAccessException: Access to the path denied
这篇关于在File.Copy C#UnauthorizedAccessException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!