0

Recently I got very strange issue with IO, I noticed that when I change project configuration from "Any CPU" to x64 or x86 input output stop working. I can't create any files even this simple code not working.

File.WriteAllText(@"CSVTEST.csv","test");

Not to mention another code which I use to download CSV file from 'nasdaq' website.

using (FileStream fileStream = new FileStream("StockHistoryData.csv", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.nasdaq.com/api/v1/historical/" + symbol + "/stocks/2010-11-14/2020-11-14");
    request.Method = WebRequestMethods.Http.Get;
    request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

    const int BUFFER_SIZE = 16 * 1024;

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        using (var responseStream = response.GetResponseStream())
        {
            var buffer = new byte[BUFFER_SIZE];
            int bytesRead;

            do
            {
                bytesRead = responseStream.Read(buffer, 0, BUFFER_SIZE);
                fileStream.Write(buffer, 0, bytesRead);
            } while (bytesRead > 0);
        }
    }
} 

I don't get any errors or warnings, files just are not being created.

I was thinking to change project configuration back to "Any CPU" but unfortunately I can't do it because I am using Microsoft ML library which works only on x64 or x86 configurations. So it's kinda impossible for me to stay on "Any CPU" configurations.

Maybe someone already had this problem, on internet I didn't find any information about this.

I also was thinking about change configuration dynamically but I think it's kinda bad coding. Not to mention this will not work stable

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Vlad
  • 419
  • 1
  • 10
  • 21
  • What do you mean exactly by "not working"? – Vlad DX Nov 22 '20 at 16:01
  • They simply not creating csv files as they did when configuration was on Any CPU. No errors or warnings just don't create files. – Vlad Nov 22 '20 at 16:06
  • 2
    Maybe you are checking the wrong place? Try to use an absolute path, check if it works. – Vlad DX Nov 22 '20 at 16:12
  • 1
    It worked) Thank you so much i would never thought that different configurations will be using another file path. – Vlad Nov 22 '20 at 16:34

1 Answers1

0

Turns out when you change project configuration you should use absolute path even if files are in project folder Worked for me. File.WriteAllText(@"C:\Users\VladMishyn\Desktop\DIPLOM1\ASIX\ASIX\bin\Debug\CSVTEST.csv", "test");

Vlad
  • 419
  • 1
  • 10
  • 21
  • 1
    1. Use absolute file paths always. 2. Use [`GetFolderPath`](https://stackoverflow.com/q/634142/22437) instead of hard-coding file paths. – Dour High Arch Nov 22 '20 at 17:20