【问题标题】:Why am I getting "could not find file '\[filename]'"?为什么我得到“找不到文件'\ [文件名]'”?
【发布时间】:2014-09-15 22:08:27
【问题描述】:

使用此代码:

private void menuItem2_Click(object sender, System.EventArgs e)
{
    String xmlFile = "DuckbilledPlatypiGuy.xml";
    String uri = "http://192.168.125.50:21608/api/inventory/sendXML/duckbill/platypus/testfyle";
    RESTfulMethods rm = new RESTfulMethods();
    rm.SendXMLFile(xmlFile, uri, 500);
}

public void SendXMLFile(string xmlFilepath, string uri, int timeout)
{
    StringBuilder sb = new StringBuilder();
    using (StreamReader sr = new StreamReader(xmlFilepath))
    {
        . . .

...我得到了,“找不到文件 '\DuckbilledPlatypiGuy.xml'

为什么要在文件名前面加上一个回击?是这个问题吗?如果是,我该如何预防?

该文件与 .exe 位于同一文件夹中,因此它应该是显而易见的。

更新

我试过了:

String xmlFile = "\\DuckbilledPlatypiGuy.xml";

...但它没有区别 - 仍然得到,“找不到文件'\DuckbilledPlatypiGuy.xml'”所以无论我给文件名没有重击或两个,它似乎仍然认为它有一个。

还尝试了以下方法,结果相同:

String xmlFile = @"DuckbilledPlatypiGuy.xml";

更新 2

在我的 .NET Compact Framework 书(第 338 页,Andy Wigley 撰写的书)中找到此内容后:

StreamReader myReader = new StreamReader("\\myFile.txt");

...我试过这个:

using (StreamReader sr = new StreamReader("\\" + xmlFilepath))

...甚至这个:

using (StreamReader sr = new StreamReader("\\DuckbilledPlatypiGuy.xml"))

...但我仍然收到相同的错误消息。

更新 3

如果我这样做:

String fallName = String.Format("\\{0}", xmlFilepath);
MessageBox.Show(String.Format("fallName is {0}", fallName));
using (StreamReader sr = new StreamReader(fallName))

...我看到“fallName is '\DuckbilledPlatypiGuy.xml'”并且我得到了相同的旧错误消息。

如果我这样做(似乎预计文件名前面会加倍回击):

String fallName = String.Format("\\\\{0}", xmlFilepath);
MessageBox.Show(String.Format("fallName is {0}", fallName));
using (StreamReader sr = new StreamReader(fallName))

...我看到“fallName is '\DuckbilledPlatypiGuy.xml'”然后我得到一个 IOException。所以文件名最终被接受(在 IO 异常之前)?奇怪……或者这里有什么别的东西在起作用?

更新 4

在那之后我根本没有做到这一点 - 我什至从未看到使用此代码的“Made it point 1”:

public void SendXMLFile(string xmlFilepath, string uri, int timeout) // timeout should be 500
{
    StringBuilder sb = new StringBuilder();
    MessageBox.Show(String.Format("xmlFilepath is {0}", xmlFilepath));
    String fallName = String.Format("\\\\{0}", xmlFilepath);
    MessageBox.Show(String.Format("fallName is {0}", fallName));
    using (StreamReader sr = new StreamReader(fallName))
    {
        String line;
        while ((line = sr.ReadLine()) != null)
        {
            sb.Append(line); 
            sb.Append("\r\n");
        }
    }
    MessageBox.Show("Made it to point 1");
    string strData = @sb.ToString();
    strData = strData.Replace("\"", "'");
    string body = String.Format("\"{0}\"", strData);
    MessageBox.Show("Made it to point 2");
    CreateRequestNoCredentials(uri, HttpMethods.POST, body, "application/json"); 
    MessageBox.Show("Made it to point 3");
}

更新 5

好的,将文件访问代码更改为此(从“我的文档”文件夹中获取文件,而不是从 .exe 所在文件夹中获取文件):

    StreamReader sr = new StreamReader(@"\My Documents\desktop.ini");

...允许我避免错误消息,但我仍然没有到达服务器中的断点。

整个代码是:

private void menuItem2_Click(object sender, System.EventArgs e)
{
    String xmlFile = "DuckbilledPlatypiGuy.xml";
    String uri = "http://192.168.125.50:21608/api/inventory/sendXML/duckbill/platypus/testfyle";
    RESTfulMethods rm = new RESTfulMethods();
    rm.SendXMLFile(xmlFile, uri, 500);
}

public void SendXMLFile(string xmlFilepath, string uri, int timeout) // timeout should be 500
{
    StringBuilder sb = new StringBuilder(); 
    StreamReader sr = new StreamReader(@"\My Documents\desktop.ini");
    String line;
    while ((line = sr.ReadLine()) != null)
    {
        sb.Append(line);
        sb.Append("\r\n");
    }
    sr.Close();

    MessageBox.Show("Made it to point 1");
    string strData = @sb.ToString();
    strData = strData.Replace("\"", "'");
    string body = String.Format("\"{0}\"", strData);
    MessageBox.Show("Made it to point 2");
    CreateRequestNoCredentials(uri, HttpMethods.POST, body, "application/json"); 
    MessageBox.Show("Made it to point 3");
}

public HttpWebRequest CreateRequestNoCredentials(string uri, HttpMethods method, string data, string contentType)
{
    WebRequest request = WebRequest.Create(uri);
    request.Method = Enum.ToObject(typeof(HttpMethods), method).ToString();
    request.ContentType = contentType;
    ((HttpWebRequest)request).Accept = contentType;
    ((HttpWebRequest)request).KeepAlive = false;
    ((HttpWebRequest)request).ProtocolVersion = HttpVersion.Version10;

    if (method != HttpMethods.GET && method != HttpMethods.DELETE)
    {
        byte[] arrData = Encoding.UTF8.GetBytes(data);
        request.ContentLength = arrData.Length;
        using (Stream oS = request.GetRequestStream())
        {
            oS.Write(arrData, 0, arrData.Length);
        }
    }
    else
    {
        // If we're doing a GET or DELETE set ContentLength to zilch
        request.ContentLength = 0;
    }
    return request as HttpWebRequest;
}

这样修饰的服务器代码:

[Route("api/inventory/sendXML/{userId}/{pwd}/{filename}")]

...未到达/未命中断点。

更新 6

饼干的关键是在命令提示符下添加:

netsh http add urlacl url=http://shannon2:80/ user=everyone

...或者这个:

netsh http add urlacl url=http://shannon2:8080/ user=everyone

有关更多详细信息,请参阅更新 5 here

【问题讨论】:

  • 您是否在带有 .exe 的文件夹中看到此文件?
  • 是的,它在同一个文件夹中。

标签: c# file-io compact-framework streamreader .net-1.1


【解决方案1】:

尝试像这样映射路径:

string appPath = Application.StartupPath;
string filePath = "DuckbilledPlatypiGuy.xml";
string fullpath = Path.Combine(appPath, filePath);

【讨论】:

  • 我添加了几个标签; StartupPath 在 .NET 1.1 / Compact Framework 中不可用。不过还是谢谢...
猜你喜欢
  • 1970-01-01
  • 2020-09-26
  • 2021-01-07
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-01
  • 2018-02-14
相关资源
最近更新 更多