1

This is my first windows form application.

I need to work with folders that I have created in my project and I need to access the Data folder where I put .txt files.

I try :

string fileName = @"Data\TextFile1.txt";
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);

but i keep receiving this error :

impossible find part of path.

How can I combine the folder's path with file's name so when I release the project all works well?

This is what I do in an asp.net application:

Path.Combine(HttpRuntime.AppDomainAppPath, "Folder/FileName.txt");
Lewis86
  • 511
  • 6
  • 15
MeJonata
  • 25
  • 1
  • 6
  • 5
    Use the debugger to see what is the value of your path variable. It is not what you expect it to be. – Steve May 14 '18 at 13:38
  • Is the directory created when you try to access the file? – Julo May 14 '18 at 13:39
  • BaseDirectory return: C:\Users\myname\Documents\Visual Studio 2015\Projects\projectName\projectName\bin\debug – MeJonata May 14 '18 at 13:41
  • to works well i need C:\Users\myname\Documents\Visual Studio 2015\Projects\projectName\projectName\Data\textfile but how can i have that ? – MeJonata May 14 '18 at 13:42
  • Store the location of your datafolder in the app.config file under the key AppSettings and read it at runtime using the ConfigurationManager.AppSettings class – Steve May 14 '18 at 13:46
  • It sounds like the file isn't there. In your project, if you have the file in your Data folder, right click on the file and make sure the Compile option is set to Content so that the file is copied to the folder during runtime. I agree with the comment above, place a break point in the code, check the full path it gets when you make it, and then go verify that file and folder exists in that location. Remember in a project it will be bin/debug... Or bin/release.... Depending and if that works, nothing hardcoded, it will almost definitely work when deployed as well. – Michael Puckett II May 14 '18 at 13:53
  • @MeJonata, Do you resolved the problem by that provided solutions by people ? if yes please accept consider answer, if no please tell us why ? – Aria May 14 '18 at 14:13

4 Answers4

1

Your data files need to be available in output folder along with you application .exe file. to do that:

  1. Open properties of each file in Data folder.
  2. Select Copy Always against Copy to output directory
  3. Then build application

This will copy Data folder along with all files in Bin\Debug folder and will work with your existing code.

enter image description here

Sandeep
  • 333
  • 2
  • 7
0

in C# .NET you can easy use the Environment Properties when working with Forms.

If you want e.g. the Appdata Path do this:

string MyPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\" + MyFileName;

But please get sure that the File/Path Exists! For this you can use File.Exists -Function:

if(File.Exists(MyPath))
{
//Do Something
}

(For File-Class you need the System.IO namespace!)

EDIT:

Example:

  string MyPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\MyConfig.txt";

if(File.Exists(MyPath))
{
 MessageBox.Show($"{MyPath} exists!");
}

Hope that helped ;)

0

If I understand correctly, you are trying to read some file you have added to your solution in Visual Studio. First, to have those files "deployed", click on them in the Solution Explorer, go to the Properties tool and have a look at the "Copy to Output Directory". Default is "Do not copy". Change that setting to "Copy always" or "Copy if newer". Now your files will be copied to the output directory when you build the solution. Then, to get the current assembly path at runtime, have a look at: Getting the path of the current assembly

string path = (new System.Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath;

from the assembly path, you can get the containing folder's path, then you can use Path.Combine() to get your desired file path.

PSkalka
  • 137
  • 5
  • My problem is simple... i have several classes with method that read a strem using (FileStream stream = new FileStream(path, FileMode.Append, FileAccess.Write)) { – MeJonata May 14 '18 at 14:04
  • All works well but the path is so String path = @"C:\Users\me\Documents\Visual Studio 2015\Projects\nameProj\nameProj\Data\fileName.bin"; – MeJonata May 14 '18 at 14:06
  • in a web site i simple do Path.Combine(HttpRuntime.AppDomainAppPath, "Folder/FileName.txt"); – MeJonata May 14 '18 at 14:06
  • what i have to do ? – MeJonata May 14 '18 at 14:07
  • @MeJonata, you're almost there. Have a look at the answer I posted above, they explain how to get the current execution path correctly. Then you can use some utility classes in the System.IO namespace, like FileInfo, DirectoryInfo, Path, and File, and you will easily read and write where you need. – PSkalka May 14 '18 at 14:23
0

I would not create any path inside the application if possible. The NET framework provides the application configuration file for this kind of problems. You should simply add this section to your configuration file (app.config when developing, then application.exe.config after compilation)

<configuration>
  <appSettings>
    <add key="mydatafolder" value="e:\whateverpathyoulike"></add>
  </appSettings>
</configuration>

Then at runtime read that value from your code

string path = ConfigurationManager.AppSettings["mydatafolder"];
path = Path.Combine(path, fileName);

This is easily modifiable both automatically or manually to adapt to any environment you find on the destination computers

Steve
  • 213,761
  • 22
  • 232
  • 286