I have a back up folder and a main folder in my project. I am giving an option to user in main menu to open file from backup folder. For t开发者_开发知识库hat i have written code:
string[] paths;
string fullpath = "";
paths = Directory.GetFiles(backuppath);
foreach (string backupfilepath in paths)
{
if (path.EndsWith(".txt"))
fullbackupfilepath = backupfilepath;
}
Here backuppath is the path of the backup file. Now i copy this back up file in main project folder:
File.Copy(fullbackupfilepath, mainprojectfilepath);
But i get the error that Could not find file C:\Users\me\Desktop\csharpproject\bin\debug\BackupProject.txt
Why is it going in bin debug folder when my project is in MyDocuments\Myproject. There is backupfolder inside this Myproject folder. Please help with a solution.
If you use relative path rather than absolute path, in runtime the base folder is the folder where the executable is located. In debug mode it's bin/debug.
It's not advisable to have user files in the programs directory at all. I.e. if you install your program in Windows Vista or 7 programms folder, you will need admin rights to write into this directory.
You should have a look at Environment.SpecialFolder
.
string yourAppPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"YourApp");
This piece of code will generate a path for the current user, where you could store the files.
When you run your program from Visual Studio, it gets compiled into the /bin/debug
subfolder of the project, and it gets run from there. That gets reflected in your paths inside your application as well.
You can add the file to your project just like your code files, and in it's properties select Copy to output directory: Copy if newer' to make sure that the file is present in the
bin/debug` folder as well. Optionally you can copy the file in there manually if you do not want to make it part of your project.
In debug mode, a application directory is a same with executable file(.exe) directory. So the backupfolder
is needed to add to bin folder, but it is valid only you and after publishing to some users, they cannot catch the backupfolder
.
I think one of a solution is that the backupfolder
with files you want to use is added to visual studio project and then set 'Do not copy' option of that files to 'Copys always'. It could be assured of a backupfolder
's path after publishing, if the backupfolder
is included to deployment.
精彩评论