1

Couple prerequisites to this question first. I'm not a native speaker, I started to learn C# today with almost no prior programming knowledge, I want to write windows form program that will copy files (graphics assets) that I include with the program to certain folders in another programs directory (to be more precise I w want to modify graphic assets with a click of a button and then be able to change those back). I did my research and I know that I should use File.Copy method I just have one question about code I found:

static void Main()
{
    string fileName = "test.txt";
    **string sourcePath = @"C:\Users\Public\TestFolder";
    string targetPath =  @"C:\Users\Public\TestFolder\SubDir";**

    // Use Path class to manipulate file and directory paths.
    string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
    string destFile = System.IO.Path.Combine(targetPath, fileName);

I know this is not complete code but this is the part I want to ask my question about. Can I define sourcePath and targetPath like this for example: "./folder/folder2/"? Basically I want my program to assume that it's in "correct directory" of another software and I don't want to specify entire path to that directory. In other words is my program aware of which directory it's in currently and if not how can I make it aware? I hope I phrased that correctly. Thank you for your help.

Forc3ofWill
  • 71
  • 1
  • 2
  • 9

2 Answers2

2

To be sure that you're referring to the application's path you can use this:

string exePath = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetEntryAssembly().Location);

Or AppDomain.CurrentDomain.BaseDirectory

Remeber to verify that the directory you're reffering to exists or otherwise you might get a DirectoryNotFoundException

Yoav
  • 3,326
  • 3
  • 32
  • 73
2

You can provide a relative path to I/O operations, and the default path is the Environment.CurrentDirectoy value, and mostly this will be the directory where your Windows Forms executable is working from.

Note that you don't need the ./ starting point in your path, File.Copy("FileA.txt", "FileA_1.txt") will look for these locations in Environment.CurrentDirectory.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
  • Thanks a lot. I'll upvote your comment as soon as I gain reputation necessary to do that. – Forc3ofWill Sep 14 '14 at 14:32
  • @Forc3ofWill No problem. My goal here is assisting you, reputation is in a secondary place ;) – Matías Fidemraizer Sep 14 '14 at 14:33
  • I have follow up question. So you're saying that default path for I/O operations is Environment.CurrentDirectoy which means I don't have to put that value in my code right? Also I don't need a ./ starting point in my path but what if files I want to copy aren't in the exact folder my program is in? So for example Folder1/Folder2/FileA.txt ? – Forc3ofWill Sep 15 '14 at 19:57
  • @Forc3ofWill Yes, you're right. About the deep path... yeah, obviously you need to specify the relative path to the file starting from `CurrentDirectory` – Matías Fidemraizer Sep 15 '14 at 20:22
  • @Forc3ofWill Now I'm not sure if File.Copy supports wildcards. For example "**\File.txt" (recursive path) – Matías Fidemraizer Sep 15 '14 at 20:24