Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c#中设置路径?_C#_Visual Studio 2010 - Fatal编程技术网

如何在c#中设置路径?

如何在c#中设置路径?,c#,visual-studio-2010,C#,Visual Studio 2010,我的解决方案中有一个名为Template的文件夹。我希望将一些文件复制到其中并从中访问。我该如何设置这条路径?部署应用程序时,此文件夹是否在那里 这行吗 File.Move(@"DebriefReportTemplate.docx", @"~\Template\DebriefReportTemplate.docx"); 编辑:此答案适用于ASP.NET应用程序 如果模板文件夹(包括其内容)是web项目的一部分,则部署应自动工作。如果要在运行时将文件添加到此文件夹,可以使用 Server.Map

我的解决方案中有一个名为Template的文件夹。我希望将一些文件复制到其中并从中访问。我该如何设置这条路径?部署应用程序时,此文件夹是否在那里

这行吗

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

编辑:此答案适用于ASP.NET应用程序

如果模板文件夹(包括其内容)是web项目的一部分,则部署应自动工作。如果要在运行时将文件添加到此文件夹,可以使用

Server.MapPath(@"~\Template\DebriefReportTemplate.docx")
,但要小心,web应用程序通常在对本地资源访问有限的标识下运行


如果你有Win应用程序,同样的情况也适用。您需要做的是将文件夹和文件作为内容添加到项目中。不过,您需要一个安装项目。

除非您在安装时构建一个安装/部署项目来创建它,或者在应用程序中添加代码以在第一次调用时创建它,否则不会创建它。

如果您担心模板文件夹的存在,您可以在代码中的某个点创建它

string path = System.IO.Path.Combine("", "Template");
System.IO.Directory.CreateDirectory(path);
然后移动文件

File.Move(@"DebriefReportTemplate.docx", @"Template\DebriefReportTemplate.docx");
你可以用

    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);
参考资料:


在帖子中没有提到asp.net,那么为什么人们会认为他正在使用它呢?你是对的,但是为什么人们会使用@作为本地路径呢?如果不是ASP.NET,我欠你一杯啤酒:)是win forms应用程序的家伙们!好了,大卫W,我欠你一杯啤酒:)我能知道如何获取包含EXE文件的dorectory的路径吗???(不是exe路径。它应该是包含exe的目录)它是web应用程序还是windows应用程序?是,它是windows应用程序如何获取cuurent应用程序目录(包含exe文件的目录)?上述代码应在包含.exe文件的文件夹中创建文件夹。如果出于其他目的需要该路径,可以在此处查看:
path.GetDirectoryName(Application.ExecutablePath)
返回应用程序的当前执行路径。