为什么我得到"找不到文件' \ [文件名]'"?

时间:2014-09-15 22:08:28

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

使用此代码:

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是'\ DuckbilledPlatypiGuy.xml'”,我得到了同样的旧错误信息。

如果我这样做(似乎期望在文件名前面加倍反击):

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

...我看到“fallName是'\ 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

1 个答案:

答案 0 :(得分:1)

尝试映射这样的路径:

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