C # writes a file in Visual Studio, but not when I publish it

I have the following code that should go td on each of them through the org list, call the toString method for each of them and print the result both on the console and on a file called Debug1.tab.

try { StreamWriter print = File.CreateText("Debug1.tab"); Console.WriteLine(LinkedInClass.isThrottled); int p = 1; foreach (Org org in orgList) { try { if (org.numContacts > 0) { Console.WriteLine(org.ToString()); print.WriteLine(org.ToString()); } } catch (Exception) { Console.WriteLine(e.StackTrace); Console.WriteLine(e.Message); Console.ReadKey();} } print.Close(); Console.WriteLine("There were " + orgList.Count + " organizations in the list." + LinkedInClass.numWithContacts + " of which I found contacts for. Throttling was "+(LinkedInClass.isThrottled?"":"not ")+"encountered."); break; } catch (Exception e) { Console.WriteLine(e.StackTrace); Console.WriteLine(e.Message); Console.ReadKey(); } 

It works fine in Visual Studio, but when I publish it, the program does not create the file or write it. It still writes to the console, catch statements are not executed, and immediately after closing StreamWriter, it correctly prints to the console.

+4
source share
2 answers

As you specify the file name (without specifying the path), the file is created in the current working directory, which may differ from the directory in which your application is located. This can help use the search on your disk to see if the file was created elsewhere.

In any case: specify the path when creating the file, so that it is always in the place where you expect it (and do not use the Program Files folder, but some kind of public folder for writing).

+2
source

You must grant full permission to your hasty user in the folder where you are going to write files. To do this, right-click on the folder, go to properties and go to the securitty tab and add curent user if it does not exist and provide full permission for this user.

0
source

All Articles