2

With this code:

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))
    {
        . . .

...I'm getting, "Could not find file '\DuckbilledPlatypiGuy.xml'"

Why is it prepending a backwhack to the file name? Is that the problem and if so, how do I prevent it?

The file is in the same folder as the .exe, so it should be in plain sight.

UPDATE

I tried this:

String xmlFile = "\\DuckbilledPlatypiGuy.xml";

...but it makes no difference - still get, "Could not find file '\DuckbilledPlatypiGuy.xml'" So whether I give the file name no whacks or two, it still seems to think it has one.

Also tried the following, with the same result:

String xmlFile = @"DuckbilledPlatypiGuy.xml";

UPDATE 2

After finding this in my .NET Compact Framework book (p. 338, the book written by Andy Wigley):

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

...I tried this:

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

...and even this:

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

...but I still get that same err msg.

UPDATE 3

If I do this:

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

...I see "fallName is '\DuckbilledPlatypiGuy.xml'" and I get the same old err msg.

If I do this (it seems to expect doubled backwhacks prepended to the filename):

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

...I see "fallName is '\DuckbilledPlatypiGuy.xml'" and then I get an IOException. So the file name is finally being accepted (prior to an IO Exception)? Bizarre...Or is something else at play here?

UPDATE 4

And I'm not making it far at all after that - I never even see "Made it point 1" with this code:

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");
}

UPDATE 5

Okay, changing the file access code to this (getting the file from the "My Documents" folder, rather than a file from the folder where the .exe lives):

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

...allows me to avoid err msgs, but I still do not reach the breakpoint in my server.

The entire code is:

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;
}

Server code decorated this way:

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

...is not reached/breakpoint not hit.

UPDATE 6

The crux of the biscuit was adding at the command prompt either this:

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

...or this:

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

See Update 5 here for more details

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

1 Answers1

2

Try to map the path like this:

string appPath = Application.StartupPath;
string filePath = "DuckbilledPlatypiGuy.xml";
string fullpath = Path.Combine(appPath, filePath);
yazanpro
  • 4,512
  • 6
  • 44
  • 66