Why am I getting "could not find file '\[filename]'"?

17,224

Try to map the path like this:

string appPath = Application.StartupPath;
string filePath = "DuckbilledPlatypiGuy.xml";
string fullpath = Path.Combine(appPath, filePath);
Share:
17,224
B. Clay Shannon-B. Crow Raven
Author by

B. Clay Shannon-B. Crow Raven

My novel about climate change and social justice featuring talking animals traveling through time and space to prevent disasters is now available on amazon, in three formats: Taterskin & The Eco Defenders Kindle eBook; Taterskin & The Eco Defenders Paperback; Taterskin & The Eco Defenders Hardcover Taterskin & The Eco Defenders, told in “first canine” by the titular character, a Labrador Retriever, is the story of a few humans and several talking animals who travel through time and space to make the past—and thus the future—a better place. The improvements effected by the Eco Defenders benefit not just the earth itself, but also mistreated humans and animals. In Book 1 (“Wonders Never Cease”), The Eco Defenders travel 150 million years into the past, to meet a Pterodactyl and make plans to “nip Nazism in the bud.” After that, it's on to 1787 Australia to protect the indigenous people and the environment there. The Eco Defenders next go to India, where they assemble animals from all over that country to put an end to Thuggee and fights to the death between Cobras and Mongooses. Their final stop is 1885 Africa, where the Eco Defenders band together with the local animals to prevent King Leopold of Belgium from taking control of the Congo, following which they put an end to the poaching of animals throughout the continent. Book 2 (“Tell it to Future Generations”) takes up with the Eco Defenders following up on their earlier adventures by 1) Preventing the American Civil War in 1861, after which a slave they free joins them; 2) Saving the Indians from being massacred at Wounded Knee in 1890, following which Chapawee, a Sioux Indian, joins the Eco Defenders; 3) Putting an end to the practice of vivisection (experimentation on live animals) in 1903; 4) Coming to the aid of exploited workers in 1911 Manhattan, saving hundreds from the Triangle Shirtwaist Fire; and 5) Traveling to the Amazon Basin in 1978 to protect and preserve the Amazon rainforest. @@@@@@@@@@@@@@@@@@@@@@@ I have lived in eight states; besides my native California (where I was born and where I now again reside), in chronological order I have infested: New York (Brooklyn), Montana (Helena), Alaska (Anchorage), Oklahoma (Bethany), Wisconsin (New Berlin and Oconomowoc), Idaho (Coeur d'Alene), and Missouri (Piedmont). I am a writer of both fiction (for which I use the nom de guerre "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006 and can be downloaded gratis here.

Updated on September 21, 2022

Comments

  • B. Clay Shannon-B. Crow Raven
    B. Clay Shannon-B. Crow Raven less than a minute

    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

  • B. Clay Shannon-B. Crow Raven
    B. Clay Shannon-B. Crow Raven about 8 years
    I added a couple more tags; StartupPath is not available in .NET 1.1 / Compact Framework. Thanks anyway, though...