Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, December 28, 2007 11:54 AM
Hi,
I'm handling a windows application. and I've few queries as follows
1. How to access files from a folder inside the .NET solution.
i tried with (@"..//Folder//Folder") - doesn't work
2. How to store a file into the bin folder of the project. i.e ( bin//debug)
Please help me
Regards,
Vino.
All replies (6)
Friday, December 28, 2007 3:27 PM âś…Answered
In most cases you will get the same result from both MessageBoxes.
Code Block
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(Application.StartupPath);
MessageBox.Show(Environment.CurrentDirectory);
}
If you build as a debug release, you will get "../../YourProject/.../bin/debug" from ApplicationStartupPath. To access files within C# projects and solutions, you can treat them as resources.
You can add the file as a resource. The contents of a text file could then be accessed like this.
Code Block
private string fileName = Properties.Resources.YourTextFileName;
Or, add the filename as a setting, if you wish. Highlight the file in Solution Explorer, select Copy To OutPut Directory. Then you can get the full path/file name to the file like this.
Code Block
private string startPath = Application.StartupPath;
// returns path to current output folder
private string fileName = Properties.Settings.Default.YourTextFileName;
// returns name of YourTextFileName.
private string CompletePathName = startPath + fileName;
Take your pick. Hope this helps.
Rudedog
Friday, December 28, 2007 1:55 PM
By accessing a file im guessing you mean open it and maybe read it? Assuming its just text a streamreader couldnt be easier.
using System.IO;
StreamReader r = new Streamreader("C:\somepath\somefile.txt");
r.read();Storing a file to any folder is the same thing with a streamwriter if its just plain text file.
Friday, December 28, 2007 2:08 PM
Hi,
Thanks for the reply.
I'm fine with accessing files and folders from the local drives.I mean the way you said,using streamreader and writer.
But my concern is to access files and folders inside the .NET solution(project).
Regards,
Vino.
Friday, December 28, 2007 2:25 PM
Environment.CurrentDirectory will give you the path of the folder in which your application is stored so you can access subfolders and files easily.
Hope this helps.
Friday, December 28, 2007 2:44 PM
Hi Moayad,
Thanks for the reply.and I still wanted to explain you the right query.
I'm a web developer and i'm doing windows for the first time.
In web, if i've to write a file into the project folder, i use (@"../../demo.doc") so, my new doc is stored inside the project(.NET solution).
And the same, how do i put files into the bin folder of the windows application.
Please help me.
Regards,
Vino
Saturday, December 29, 2007 2:18 PM
Brilliant,Its exactly what I asked for.
Thank you very much.