简体   繁体   中英

C# StreamWriter , write to a file from different class?

How can I write to a file from different class?

public class gen
{
   public static string id;
   public static string m_graph_file;
}

static void Main(string[] args)
{
  gen.id = args[1]; 
  gen.m_graph_file = @"msgrate_graph_" + gen.id + ".txt";
  StreamWriter mgraph = new StreamWriter(gen.m_graph_file);
  process();
}

public static void process()
{
  <I need to write to mgraph here>
}

Pass the StreamWriter mgraph to your process() method

static void Main(string[] args)
{
  // The id and m_graph_file fields are static. 
  // No need to instantiate an object 
  gen.id = args[1]; 
  gen.m_graph_file = @"msgrate_graph_" + gen.id + ".txt";
  StreamWriter mgraph = new StreamWriter(gen.m_graph_file);
  process(mgraph);
}

public static void process(StreamWriter sw)
{
 // use sw 
}

However your code has some, difficult to understand, points:

  • You declare the class gen with two static vars. These vars are shared between all instances of gen. If this is a desidered objective, then no problem, but I am a bit puzzled.
  • You open the StreamWriter in your main method. This is not really necessary given the static m_grph_file and complicates the cleanup in case your code raises exceptions.

For example, in you gen class, (or in another class) you could write methods that work on the same file because the file name is static in the class gen

public static void process2()
{
    using(StreamWriter sw = new StreamWriter(gen.m_graph_file)) 
    { 
        // write your data .....
        // flush
        // no need to close/dispose inside a using statement.
    } 
}

You can pass a StreamWriter object as a parameter. Alternatively you could create a new instance inside your process method. I would also recommend wrapping your StreamWriter inside a using :

public static void process(StreamWriter swObj)
{
  using (swObj)) {
      // Your statements
  }
}

Of course you could simply have your 'process' method like this:

public static void process() 
{
  // possible because of public class with static public members
  using(StreamWriter mgraph = new StreamWriter(gen.m_graph_file))
  {
     // do your processing...
  }
}

But from the design point of view this would make more sense (EDIT: full code):

public class Gen 
{ 
   // you could have private members here and these properties to wrap them
   public string Id { get; set; } 
   public string GraphFile { get; set; } 
} 

public static void process(Gen gen) 
{
   // possible because of public class with static public members
   using(StreamWriter mgraph = new StreamWriter(gen.GraphFile))
   {
     sw.WriteLine(gen.Id);
   }
}

static void Main(string[] args) 
{ 
  Gen gen = new Gen();
  gen.Id = args[1];  
  gen.GraphFile = @"msgrate_graph_" + gen.Id + ".txt"; 
  process(gen); 
}

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