C# 正在 Visual Studio 中编写文件,但在我发布文件时却没有

标签 c# file-io publishing

我有以下代码,应该通过组织列表对每个组织进行 td,对每个组织调用 toString 方法,并将结果打印到控制台和名为 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(); }

在 Visual Studio 中,它运行良好,但当我发布它时,程序不会创建文件或写入文件。它仍在写入控制台,catch 语句未执行,并且在它应该关闭 streamWriter 后立即正确打印到控制台。

最佳答案

你指定文件名的方式(不给出路径),文件是在当前工作目录中创建的,这可能与你的应用所在的目录不同。使用search your disk可能有助于查看文件是否在别处创建。

无论如何:在创建文件时指定一个路径,以确保它始终位于您期望的位置(并且不要使用 Program Files 文件夹,而是一些可公开写入的文件夹)。

关于C# 正在 Visual Studio 中编写文件,但在我发布文件时却没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14278400/

相关文章:

c# - 如何使用 .net 核心默认服务容器注册特定接口(interface)的所有类

java - Double BufferedWriter 或 BufferedReader 的优点

java - 变量可能还没有初始化?

c# - 同步 XmlWriter 对文件的访问以防止 IOExceptions 的最佳方法是什么?

sharepoint - MOSS 最小发布站点定义

C# 屏幕截图 winlogon 以及用户桌面

c# - 如何联合查询复杂对象的两个复杂查询?

c# - 如何使用 C# 获取 MMM、YYYY 格式的日期

visual-studio-2010 - VS2010 "Copy to output directory"无法正常工作?

ios - 尝试使用 Flash CS5.5 发布 AIR for iOS 应用程序,但未生成 ipa