0

I have a folder named Template in my solution. I want some files to be copied in to it and accessed from it. How can i set the path to that? Will this folder be there when i deploy the application?

does this work?

File.Move(@"DebriefReportTemplate.docx", @"~\Template\DebriefReportTemplate.docx");
2
  • Is it a web application or windows application? Commented May 19, 2012 at 11:15
  • yes it is a windows application Commented May 19, 2012 at 11:53

4 Answers 4

1

It won't be created unless you either build a setup/deployment project to create it at install time, or add code in your app to create it upon first invocation.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are worried about the existence of the Template folder, you could just create it at some point in your code.

string path = System.IO.Path.Combine("", "Template");
System.IO.Directory.CreateDirectory(path);

and then move the file

File.Move(@"DebriefReportTemplate.docx", @"Template\DebriefReportTemplate.docx");

2 Comments

How to get the cuurent application directory(directory that had the exe file)??
The above code should create the folder in the folder containing the .exe file. If you need the path for other purposes you could look here: link
0

EDIT: This answer is for an ASP.NET application.

If Template folder (including its content) is part of the web project, the deployment should work automatically. If you want to add files to this folder at runtime, you can use

Server.MapPath(@"~\Template\DebriefReportTemplate.docx")

, but be careful, the web application usually runs under an identity which has limited access to the local resources.

The same thing applies if you have a Win app. What you need to do is to add the folder and the files to the project, as Content. You will need a setup project though.

6 Comments

There's no mention of asp.net in the post, so why would one assume he's using it?
You are right, but why would one use @"~" for a local path? If it's not ASP.NET, I owe you a beer :)
It's win forms application guys!
there you go, David W, I owe you a beer :)
Can i know how to get the path to the dorectory that contains the EXE file??? (not the exe path. It should be the directory that contains the exe)
|
0

You may use

    string sourceFile = Path.GetDirectoryName(Application.ExecutablePath)+@"\Template\DebriefReportTemplate.docx";
    string destinationFile = @"C:\DebriefReportTemplate.docx";

    // To move a file or folder to a new location:
    System.IO.File.Move(sourceFile, destinationFile);

References :

1 Comment

Path.GetDirectoryName(Application.ExecutablePath) returns the current execution path of the application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.