Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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# - Fatal编程技术网

安装C#程序后如何查找文件路径?

安装C#程序后如何查找文件路径?,c#,C#,在另一台计算机上安装程序后,我在查找文件时遇到问题。我正在使用C#,这是我代码的一部分: // template path string tmpPath = @"|DataDirectory|\Templete.docx"; // output path string outputName = @"|DataDirectory|\Output.pdf"; // shadow file name string shadowFile = @"|D

在另一台计算机上安装程序后,我在查找文件时遇到问题。我正在使用C#,这是我代码的一部分:

// template path
string tmpPath = @"|DataDirectory|\Templete.docx";

// output path
string outputName = @"|DataDirectory|\Output.pdf";

// shadow file name
string shadowFile = @"|DataDirectory|\temp.docx";

// Create shadow File
File.Copy(tmpPath, shadowFile, true);

// open word
word.Application app = new word.Application();
Document doc = app.Documents.Open(shadowFile);
我使用了
“|DataDirectory |”
,它用于查找数据库,但在这里我再次使用它来查找word文件,它向我抛出以下错误:

System.ArgumentException:“路径中存在非法字符。”


您需要为DataDirectory创建一个新变量

string datadirectory = "c:\pathtodatadirectory";

string tmpPath = datadirectory + @"\Templete.docx";
// output path
string outputName = datadirectory + @"\Output.pdf";
// shadow file name
string shadowFile = datadirectory + @"\temp.docx";
显然,您希望数据目录是动态的。从OS变量获取目录路径


请看这篇文章

我建议使用带有
Assembly.GetEntryAssembly().Location的反射来获取启动可执行文件的路径。您还可以使用
Path
组合路径和文件名,创建临时文件,以及执行其他公共路径操作。最后,使用
Environment.GetFolderPath()
查找我的文档所在的位置,或者
C:\Users\XXXX\AppData\Local
所在的位置以及所有其他特殊命名文件夹

using static System.IO.Path;
using static System.Environment;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var path = GetDirectoryName(Assembly.GetEntryAssembly().Location);
            var template = Combine(path, "Template.docx");
            var tempFile = Combine(GetTempPath(), "temp.docx");
            var userFile = Combine(GetFolderPath(SpecialFolder.LocalApplicationData), "UserPreferences.docx");
            var docFile = Combine(GetFolderPath(SpecialFolder.MyDocuments), "MyDocument.docx");
        }
    }
}
另一种方法是在安装期间将程序路径存储在注册表中,然后在程序运行时读取注册表


例如,将值写入
HKEY\U CURRENT\U USER\Software\Application1
或任何名称,并使用
Microsoft.Win32.Registry.CurrentUser.OpenSubKey(“Software\Application1”)

读取该值。此外,您不应使用程序目录存储数据。此用途有每个用户特定的文件夹。@johnnymapp那么,什么能帮助我在以后找到所需的文件呢installation@JohnnyMopp我在连接字符串中使用了它来查找数据库,我认为它可以用于此too@JAlex安装后我应该使用什么来查找文件我更喜欢使用
System.IO.Path.Combine()
为了解决目录分隔字符的任何格式问题。在任务计划程序或sql server代理中运行控制台应用程序会导致使用Assembly.GetEntryAssembly().Location的路径错误。最后,我使用它获得了正确的可执行路径:path.GetDirectoryName(System.Reflection.Assembly.getExecutionGassembly().GetName().CodeBase)<代码>GetExecutionGassembly()
是一种方法。剩下的只是文件路径操作。