Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.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
使用TextReader的C#文件路径默认为文档和设置_C#_Path_Windows Server 2003_Streamreader_Textreader - Fatal编程技术网

使用TextReader的C#文件路径默认为文档和设置

使用TextReader的C#文件路径默认为文档和设置,c#,path,windows-server-2003,streamreader,textreader,C#,Path,Windows Server 2003,Streamreader,Textreader,我有一个程序已经作为预定任务运行了相当长的一段时间。我对它做了一个更改,甚至与TextReader无关,并将.exe的副本放回服务器(Server2003R2 SP2)现在,当您从计划任务或双击.exe运行程序时,下面的一行将尝试从C:\Documents and Settings\user\中读取文件IPAddressMonitor.ini,而不是从C:\IPAddressMonitor中的.exe文件夹中读取。知道为什么吗 TextReader tr = new StreamReader("

我有一个程序已经作为预定任务运行了相当长的一段时间。我对它做了一个更改,甚至与TextReader无关,并将.exe的副本放回服务器(Server2003R2 SP2)现在,当您从计划任务或双击.exe运行程序时,下面的一行将尝试从C:\Documents and Settings\user\中读取文件IPAddressMonitor.ini,而不是从C:\IPAddressMonitor中的.exe文件夹中读取。知道为什么吗

TextReader tr = new StreamReader("IPAddressMonitor.ini");

使用反射获取可执行文件的路径-这样,只要.ini与可执行文件位于同一文件夹中(或与之相关的文件夹),您就不会再遇到此问题:

static public string AssemblyDirectory
{
    get
    {
        string codeBase = Assembly.GetExecutingAssembly().CodeBase;
        UriBuilder uri = new UriBuilder(codeBase);
        string path = Uri.UnescapeDataString(uri.Path);
        return Path.GetDirectoryName(path);
    }
}


有关更多信息,请参见

您是否正在执行(手动或通过编码)可能更改当前工作目录的操作?不是通过编码。你看到的就是我在跑步。我是否误解了执行.exe的目录应该是它从中提取.ini的位置?不知道为什么会发生这种情况,但您可以始终使用反射来获取.exe的路径-只要您的.ini与可执行文件位于同一文件夹中(或与之相关的某个文件夹中)你不会再遇到这个问题了。什么设置了“当前目录?”我以为它来自于执行.exe的目录。这不正确吗?是否在任务计划程序中为程序设置了路径?
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)