c# - 为什么我得到 "could not find file '\[文件名 ]'"?

标签 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 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

cookies 的关键是在命令提示符下添加以下内容:

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

...或者这个:

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

请参阅更新 5 here了解更多详情

最佳答案

尝试像这样映射路径:

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

关于c# - 为什么我得到 "could not find file '\[文件名 ]'"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25857681/

相关文章:

c# - 为什么 Java 和 C# 中的逻辑运算符和按位运算符之间存在区别?

c# - VB 和 C# 之间的绑定(bind)问题

c# - 存储当前行索引的干净实现

java - 读取文本文件时检查不可打印字符的行

c - 如何用 C 将二进制位写入二进制文件?

c# - 非 DataTable 数据源的 DataGrid MappingName 是什么?

.net - 清除组合框数据源项目

c# - 根据 Msdn GridView.DataKeyNames 必须设置,如果我们想更新数据库

java - 使用 Java 扫描器读取文件

.net - 如何在 ASP.NET Web 应用程序中为 Windows Mobile 创建 SQL Compact 3.5 数据库?