简体   繁体   中英

How to set the path in c#?

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");

除非您建立安装/部署项目以在安装时创建它,或者在您的应用程序中添加代码以在首次调用时创建它,否则它将不会被创建。

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.

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");

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 :

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM