![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | IOException - How to gain file name? Hi, in our software we're using a couple of File.Copy() or File.Move() statements. The system is supposed to throw an exception if a file already exists. We log the exception into a file then. Unfortunately, the IOException doesn't seem to provide any means to extract the destination file name that caused the exception. The message only states "Cannot create a file when that file already exists." Does anyone know a way to retrieve the file name which caused the exception? I don't want to manually track the currently used file name. I don't like the idea to extend the System.IO.File class and IOException class either... Well, as long as I don't need to... Any help is appreciated! Best regards, www.axeldahmen.de Axel Dahmen |
My System Specs![]() |
| | #2 (permalink) |
| | Re: IOException - How to gain file name? Axel Dahmen wrote: Quote: > in our software we're using a couple of File.Copy() or File.Move() > statements. The system is supposed to throw an exception if a file > already exists. We log the exception into a file then. > > Unfortunately, the IOException doesn't seem to provide any means to > extract the destination file name that caused the exception. The > message only states "Cannot create a file when that file already > exists." > > Does anyone know a way to retrieve the file name which caused the > exception? I don't want to manually track the currently used file > name. I don't like the idea to extend the System.IO.File class and > IOException class either... Well, as long as I don't need to... something you can easily check for in the first place: If Not(File.Exists(dest)) then File.Copy(src, dest) Else ' log dest to file End If That way you'll only get an IOException if an I/O error occurred. Andrew |
My System Specs![]() |
| | #3 (permalink) |
| | Re: IOException - How to gain file name? On Thu, 5 Mar 2009 12:33:03 -0000, "Andrew Morton" <akm@xxxxxx-press.co.uk.invalid> wrote: Quote: >Axel Dahmen wrote: Quote: >> in our software we're using a couple of File.Copy() or File.Move() >> statements. The system is supposed to throw an exception if a file >> already exists. We log the exception into a file then. >> >> Unfortunately, the IOException doesn't seem to provide any means to >> extract the destination file name that caused the exception. The >> message only states "Cannot create a file when that file already >> exists." >> >> Does anyone know a way to retrieve the file name which caused the >> exception? I don't want to manually track the currently used file >> name. I don't like the idea to extend the System.IO.File class and >> IOException class either... Well, as long as I don't need to... >You don't need to (and should not) rely on an exception being raised for >something you can easily check for in the first place: > >If Not(File.Exists(dest)) then > File.Copy(src, dest) >Else > ' log dest to file >End If > >That way you'll only get an IOException if an I/O error occurred. > >Andrew could disappear between the File.Exists test and the copy. Also I suspect there are cases where File.Exists works but the user does not have permission to open the file for reading. |
My System Specs![]() |
| | #4 (permalink) |
| | Re: IOException - How to gain file name? I agree. And that's exactly what the exception is made for. So I don't want to explicitly avoid such exception but to handle it gracefully, giving a trace leading to the problem file whenever this may happen. Best regards, www.axeldahmen.de Axel Dahmen ---------- "Jack Jackson" <jjackson@xxxxxx> schrieb im Newsbeitrag news:ialvq4502mv4kppvn5f84cdut3s75g5bv1@xxxxxx Quote: > On Thu, 5 Mar 2009 12:33:03 -0000, "Andrew Morton" > <akm@xxxxxx-press.co.uk.invalid> wrote: > Quote: >>Axel Dahmen wrote: Quote: >>> in our software we're using a couple of File.Copy() or File.Move() >>> statements. The system is supposed to throw an exception if a file >>> already exists. We log the exception into a file then. >>> >>> Unfortunately, the IOException doesn't seem to provide any means to >>> extract the destination file name that caused the exception. The >>> message only states "Cannot create a file when that file already >>> exists." >>> >>> Does anyone know a way to retrieve the file name which caused the >>> exception? I don't want to manually track the currently used file >>> name. I don't like the idea to extend the System.IO.File class and >>> IOException class either... Well, as long as I don't need to... >>You don't need to (and should not) rely on an exception being raised for >>something you can easily check for in the first place: >> >>If Not(File.Exists(dest)) then >> File.Copy(src, dest) >>Else >> ' log dest to file >>End If >> >>That way you'll only get an IOException if an I/O error occurred. >> >>Andrew > While that will work 99.9% of the time, it is possible that the file > could disappear between the File.Exists test and the copy. Also I > suspect there are cases where File.Exists works but the user does not > have permission to open the file for reading. |
My System Specs![]() |
| | #5 (permalink) |
| | RE: IOException - How to gain file name? Hi Axel, There is no way to extract the destination file name from the IOException if the file already exists when we call File.Move. This can be confirmed by digging into the File class's code. The File.Move class calls MoveFile function in kernel32.dll internally, so actually the Move method itself doesn't do the Move job. When the MoveFile function returns, code in Move method checks the return value, if the return value is zero, which means an error occurred during move, it then calls an internal helper method to get the last win32 error can convert it to .NET exception and throw it: if (!Win32Native.MoveFile(fullPathInternal, dst)) { __Error.WinIOError(); } The helper method WinIOError has an overload which can take two parameters, an error code and a path. But as we can see here, the Move method somehow doesn't pass in any information to the WinIOError, which causes WinIOError to pass a string.Empty as the file path: internal static void WinIOError() { WinIOError(Marshal.GetLastWin32Error(), string.Empty); } So the information you need from the exception is lost here. Being different from File.Move, the File.Copy method internally call the WinIOError this way to throw an Exception: __Error.WinIOError(errorCode, maybeFullPath); And if we do something like this (if the target file already exists): File.Copy(@"C:\Users\jiewan\Desktop\snippet.txt", @"D:\snippet.txt", false); We'll get an IOException with the following message: The file 'D:\snippet.txt' already exists. I understand this is inconvenient for your scenario, however it seems to me that we have to track the file current being used for the log: try { File.Move(srcFile, destPath); } catch (IOException ioEx) { Log(string.Format("{0} already exists.", destPath)); } Please kindly let me know if this clarifies your question on File.Move method. Regards, Jie Wang (jiewan@xxxxxx, remove 'online.') Microsoft Online Community Support Delighting our customers is our #1 priority. We welcome your comments and suggestions about how we can improve the support we provide to you. Please feel free to let my manager know what you think of the level of service provided. You can send feedback directly to my manager at: msdnmg@xxxxxx. ================================================== Get notification to my posts through email? Please refer to http://msdn.microsoft.com/en-us/subs...#notifications. Note: MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 2 business days is acceptable. Please note that each follow up response may take approximately 2 business days as the support professional working with you may need further investigation to reach the most efficient resolution. The offering is not appropriate for situations that require urgent, real-time or phone-based interactions. Issues of this nature are best handled working with a dedicated Microsoft Support Engineer by contacting Microsoft Customer Support Services (CSS) at http://msdn.microsoft.com/en-us/subs.../aa948874.aspx ================================================== This posting is provided "AS IS" with no warranties, and confers no rights. |
My System Specs![]() |
| | #6 (permalink) |
| | Re: IOException - How to gain file name? Hi Axel, There is no way to extract the destination file name from the IOException if the file already exists when we call File.Move. This can be confirmed by digging into the File class's code. The File.Move class calls MoveFile function in kernel32.dll internally, so actually the Move method itself doesn't do the Move job. When the MoveFile function returns, code in Move method checks the return value, if the return value is zero, which means an error occurred during move, it then calls an internal helper method to get the last win32 error, converts it to .NET exception and throws it: if (!Win32Native.MoveFile(fullPathInternal, dst)) { __Error.WinIOError(); } The helper method WinIOError has an overload which can take two parameters, an error code and a path. But as we can see here, the Move method somehow doesn't pass in any information to the WinIOError, which causes WinIOError to pass a string.Empty as the file path: internal static void WinIOError() { WinIOError(Marshal.GetLastWin32Error(), string.Empty); } So the information you need from the exception is lost here. Being different from File.Move, the File.Copy method internally call the WinIOError this way to throw an Exception: __Error.WinIOError(errorCode, maybeFullPath); And if we do something like this (if the target file already exists): File.Copy(@"C:\Users\jiewan\Desktop\snippet.txt", @"D:\snippet.txt", false); We'll get an IOException with the following message: The file 'D:\snippet.txt' already exists. I understand this is inconvenient for your scenario, however it seems to me that we have to track the file current being used for the log: try { File.Move(srcFile, destPath); } catch (IOException ioEx) { Log(string.Format("{0} already exists.", destPath)); } Please kindly let me know if this clarifies your question on File.Move method. Regards, Jie Wang (jiewan@xxxxxx, remove 'online.') Microsoft Online Community Support Delighting our customers is our #1 priority. We welcome your comments and suggestions about how we can improve the support we provide to you. Please feel free to let my manager know what you think of the level of service provided. You can send feedback directly to my manager at: msdnmg@xxxxxx. ================================================== Get notification to my posts through email? Please refer to http://msdn.microsoft.com/en-us/subs...#notifications. Note: MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 2 business days is acceptable. Please note that each follow up response may take approximately 2 business days as the support professional working with you may need further investigation to reach the most efficient resolution. The offering is not appropriate for situations that require urgent, real-time or phone-based interactions. Issues of this nature are best handled working with a dedicated Microsoft Support Engineer by contacting Microsoft Customer Support Services (CSS) at http://msdn.microsoft.com/en-us/subs.../aa948874.aspx ================================================== This posting is provided "AS IS" with no warranties, and confers no rights. |
My System Specs![]() |
| | #7 (permalink) |
| | Re: IOException - How to gain file name? Shouldn;t the File.Copy()/File.Move() methods takes "SourcePath" and "DestinatePath" parameters, should they? So, when your code call these methoeds, you know what the destination path is. So, if the exception bubles up to an outer wrapping procedure, you can easily re-wrap the exception at the method-calling level to incorporate the destination path and rethrow the exception: try { FileCopy(source, destination); } catch (IOException ex) { //re-throw the exception with destination file name added //You may create your own custom Exception, insteadof using generic ApplicationException throw new ApplicationException("Copying \"" + destination "\" failed: " + ex.Message); } catch(Exception e) { throw new ApplicationException("Copying \"" + destination "\" failed: " + ex.Message); } Then, in your Excpetion logging level, the re-thrown exceptpion will be caught with destination file information included. "Axel Dahmen" <keentoknow@xxxxxx> wrote in message news:%23UgmlBYnJHA.1172@xxxxxx Hi, in our software we're using a couple of File.Copy() or File.Move() statements. The system is supposed to throw an exception if a file already exists. We log the exception into a file then. Unfortunately, the IOException doesn't seem to provide any means to extract the destination file name that caused the exception. The message only states "Cannot create a file when that file already exists." Does anyone know a way to retrieve the file name which caused the exception? I don't want to manually track the currently used file name. I don't like the idea to extend the System.IO.File class and IOException class either... Well, as long as I don't need to... Any help is appreciated! Best regards, www.axeldahmen.de Axel Dahmen |
My System Specs![]() |
| | #8 (permalink) |
| | Re: IOException - How to gain file name? Why do you need IOException to tell you what the destination file was? You should already have it. string sourcePath = "Some File"; string destPath = "Another Location"; try { File.Copy(sourcePath, destPath); } catch (IOException e) { Debug.WriteLine("Error copying {0} to {1}", sourcePath, destPath); } Andrew Faust "Axel Dahmen" <keentoknow@xxxxxx> wrote in message news:ORkksbbnJHA.500@xxxxxx Quote: > I agree. And that's exactly what the exception is made for. > > So I don't want to explicitly avoid such exception but to handle it > gracefully, giving a trace leading to the problem file whenever this may > happen. > > Best regards, > www.axeldahmen.de > Axel Dahmen > > > > ---------- > "Jack Jackson" <jjackson@xxxxxx> schrieb im Newsbeitrag > news:ialvq4502mv4kppvn5f84cdut3s75g5bv1@xxxxxx Quote: >> On Thu, 5 Mar 2009 12:33:03 -0000, "Andrew Morton" >> <akm@xxxxxx-press.co.uk.invalid> wrote: >> Quote: >>>Axel Dahmen wrote: >>>> in our software we're using a couple of File.Copy() or File.Move() >>>> statements. The system is supposed to throw an exception if a file >>>> already exists. We log the exception into a file then. >>>> >>>> Unfortunately, the IOException doesn't seem to provide any means to >>>> extract the destination file name that caused the exception. The >>>> message only states "Cannot create a file when that file already >>>> exists." >>>> >>>> Does anyone know a way to retrieve the file name which caused the >>>> exception? I don't want to manually track the currently used file >>>> name. I don't like the idea to extend the System.IO.File class and >>>> IOException class either... Well, as long as I don't need to... >>> >>>You don't need to (and should not) rely on an exception being raised for >>>something you can easily check for in the first place: >>> >>>If Not(File.Exists(dest)) then >>> File.Copy(src, dest) >>>Else >>> ' log dest to file >>>End If >>> >>>That way you'll only get an IOException if an I/O error occurred. >>> >>>Andrew >> While that will work 99.9% of the time, it is possible that the file >> could disappear between the File.Exists test and the copy. Also I >> suspect there are cases where File.Exists works but the user does not >> have permission to open the file for reading. |
My System Specs![]() |
| | #9 (permalink) |
| | Re: IOException - How to gain file name? > Why do you need IOException to tell you what the destination file was? You Quote: > should already have it. Regards, www.axeldahmen.de Axel Dahmen |
My System Specs![]() |
| | #10 (permalink) |
| | Re: IOException - How to gain file name? Thanks, Norman, but doing this would contradict using exceptions at all. As I've also replied to Andrew, the fun part of exception programming is that you *don't* anticipate any exception but handle them at *one single* place - gracefully. We're using a factory that creates classes and implements a global try-catch to be able to log any errors that occurred. Adding a manual try-catch to each and every system function call would not be what's usually known as structured programming. So I guess we'll have to make our way in creating our own File class, aggregating the original one and extending its functions by local try-catch-rethrow wrappers. :/ Thanks for all your replies, guys. www.axeldahmen.de Axel Dahmen ----------- "Norman Yuan" <FakeName@xxxxxx> schrieb im Newsbeitrag news:%23L%23svhmnJHA.5980@xxxxxx Quote: > Shouldn;t the File.Copy()/File.Move() methods takes "SourcePath" and > "DestinatePath" parameters, should they? So, when your code call these > methoeds, you know what the destination path is. So, if the exception bubles > up to an outer wrapping procedure, you can easily re-wrap the exception at > the method-calling level to incorporate the destination path and rethrow the > exception: > > > try > { > FileCopy(source, destination); > } > catch (IOException ex) > { > //re-throw the exception with destination file name added > //You may create your own custom Exception, insteadof using generic > ApplicationException > throw new ApplicationException("Copying \"" + destination "\" failed: " > + ex.Message); > } > catch(Exception e) > { > throw new ApplicationException("Copying \"" + destination "\" failed: " > + ex.Message); > } > > Then, in your Excpetion logging level, the re-thrown exceptpion will be > caught with destination file information included. > > > "Axel Dahmen" <keentoknow@xxxxxx> wrote in message > news:%23UgmlBYnJHA.1172@xxxxxx > Hi, > > in our software we're using a couple of File.Copy() or File.Move() > statements. The system is supposed to throw an exception if a file already > exists. We log the exception into a file then. > > Unfortunately, the IOException doesn't seem to provide any means to extract > the destination file name that caused the exception. The message only states > "Cannot create a file when that file already exists." > > Does anyone know a way to retrieve the file name which caused the exception? > I don't want to manually track the currently used file name. I don't like > the idea to extend the System.IO.File class and IOException class either... > Well, as long as I don't need to... > > Any help is appreciated! > > Best regards, > www.axeldahmen.de > Axel Dahmen > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| unhandled IOException | Vista security | |||
| RAID5 Performance Gain? | Vista performance & maintenance | |||
| Any speed gain in 64? | Vista General | |||
| Microphone Gain | Vista General | |||