File.Copy Issue
-
Good People, I have attempted to use File.Copy to copy from a source file to a new file. However, even after I get the apps running directory (usine AppDomain.CurrentDomain.BaseDirectory + @"UserImages\") it still does not work. It throws an exception claiming it can't find part of the directory of the destination file. What I don't understand is that using AppDomain.CurrentDomain.BaseDirectory + @"Database\AppDatabase.vdb3" to find my database for the application works as expected. Do I have to create the new file first before writing to it? Doesn't File.Copy create the new file? Any ideas? Thanks, Blitz
-
Good People, I have attempted to use File.Copy to copy from a source file to a new file. However, even after I get the apps running directory (usine AppDomain.CurrentDomain.BaseDirectory + @"UserImages\") it still does not work. It throws an exception claiming it can't find part of the directory of the destination file. What I don't understand is that using AppDomain.CurrentDomain.BaseDirectory + @"Database\AppDatabase.vdb3" to find my database for the application works as expected. Do I have to create the new file first before writing to it? Doesn't File.Copy create the new file? Any ideas? Thanks, Blitz
Hi, Windows does not create the directory if it does not already exist. Windows Explorer will create directories for you, the programming interfaces don't. So you have to take care of that yourself using Directory.CreateDirectory. BTW: there is no need to test, creating a directory that already exists is harmless. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
Hi, Windows does not create the directory if it does not already exist. Windows Explorer will create directories for you, the programming interfaces don't. So you have to take care of that yourself using Directory.CreateDirectory. BTW: there is no need to test, creating a directory that already exists is harmless. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
Thanks for your reply. The directory exists, but the file doesn't. Do I need to create the file? The examples in msdn didn't indicate I need to create the file. Plus, how would I create an empty jpg file? Thanks again. Blitz
-
Good People, I have attempted to use File.Copy to copy from a source file to a new file. However, even after I get the apps running directory (usine AppDomain.CurrentDomain.BaseDirectory + @"UserImages\") it still does not work. It throws an exception claiming it can't find part of the directory of the destination file. What I don't understand is that using AppDomain.CurrentDomain.BaseDirectory + @"Database\AppDatabase.vdb3" to find my database for the application works as expected. Do I have to create the new file first before writing to it? Doesn't File.Copy create the new file? Any ideas? Thanks, Blitz
You'd better make sure the destination directory exist while coping file.Hope such code can help:
string strPath = AppDomain.CurrentDomain.BaseDirectory + @"UserImages\";
// check the directory existence
if(!Directory.Exists(strPath))
Directory.CreateDirectory(strPath);
//...your file copy code here...
// eg.
// File.Copy("d:\\a.txt", strPath + "b.txt");Regards, Dealon Impossible is nothing!
-
Good People, I have attempted to use File.Copy to copy from a source file to a new file. However, even after I get the apps running directory (usine AppDomain.CurrentDomain.BaseDirectory + @"UserImages\") it still does not work. It throws an exception claiming it can't find part of the directory of the destination file. What I don't understand is that using AppDomain.CurrentDomain.BaseDirectory + @"Database\AppDatabase.vdb3" to find my database for the application works as expected. Do I have to create the new file first before writing to it? Doesn't File.Copy create the new file? Any ideas? Thanks, Blitz
-
Good People, I have attempted to use File.Copy to copy from a source file to a new file. However, even after I get the apps running directory (usine AppDomain.CurrentDomain.BaseDirectory + @"UserImages\") it still does not work. It throws an exception claiming it can't find part of the directory of the destination file. What I don't understand is that using AppDomain.CurrentDomain.BaseDirectory + @"Database\AppDatabase.vdb3" to find my database for the application works as expected. Do I have to create the new file first before writing to it? Doesn't File.Copy create the new file? Any ideas? Thanks, Blitz
Thank you for all of your replies. What I don't understand, however, is that the folder does exist. In fact, the path it shows as not being able to find is, in fact, the correct path to the folder. I need it to place the new file in the folder - which already exists. So the directory already exists. I apologize for my confusion, but what am I missing? Thanks, Blitz
-
Thank you for all of your replies. What I don't understand, however, is that the folder does exist. In fact, the path it shows as not being able to find is, in fact, the correct path to the folder. I need it to place the new file in the folder - which already exists. So the directory already exists. I apologize for my confusion, but what am I missing? Thanks, Blitz
OMG!!! You all are so right! Thanks. Indeed, it did not exist. See, my error was that I added the "UserImages" folder to my project. However, it was not in my output directory. I failed to realize the difference. Now I know, and knowing is half the battle :-) Thanks so much for your help.