简体   繁体   中英

C# is writing a file in Visual Studio but not when I publish it

I have the following code which is supposed to go td on each of them through a list of orgs, call the toString method on each of them, and print the result to both the console and to a file named 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(); }

In Visual Studio it works perfectly but when I publish it the program doesn't create the file or write to it. It's still writing to the console, the catch statements aren't executing, and immediately after it should be closing the streamWriter it correctly prints to the console.

The way you specify the file name (without giving a path), the file is created in the current working directory, which may be different from the directory your application resides in. It may help to use the search your disk to see whether the file was created elsewhere.

Anyway: Specify a path when creating the file to make sure it's always in the location where you expect it to be (and do not use the Program Files folder, but some publically writable folder).

您应该将要写入文件的文件夹授予os curent用户的完全权限。为此,请右键单击该文件夹,然后转到属性,然后转到securitty选项卡,然后添加该curent用户(如果该文件夹不存在)并提供该用户的完全权限。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM