Advertisement

01.05.2008 at 05:01PM PST, ID: 23061400
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

I  need to create a file using c# class
Tags: c# , .net, .net2.0 framework, c#, c#, internet explorer, local
I need to write a method/class in C# that would create a file. This method/dll will be eventually be used as a reference in another program. I will also need an executable file to test it out.
Start your free trial to view this solution
Question Stats
Zone: Microsoft
Question Asked By: sfecto
Solution Provided By: FernandoSoto
Participating Experts: 2
Solution Grade: A
Views: 315
Translate:
Loading Advertisement...
01.05.2008 at 05:31PM PST, ID: 20592103

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.05.2008 at 11:33PM PST, ID: 20593086

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2008 at 02:20AM PST, ID: 20593399

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2008 at 10:11AM PST, ID: 20594834

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2008 at 11:42AM PST, ID: 20595270

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2008 at 12:29PM PST, ID: 20595473

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2008 at 12:39PM PST, ID: 20595503

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2008 at 12:43PM PST, ID: 20595514

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2008 at 12:45PM PST, ID: 20595522

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2008 at 01:25PM PST, ID: 20595676

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2008 at 05:34PM PST, ID: 20596543

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2008 at 07:23PM PST, ID: 20596937

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2008 at 08:26PM PST, ID: 20597085

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2008 at 08:54PM PST, ID: 20597141

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.06.2008 at 09:20PM PST, ID: 20597185

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.07.2008 at 07:03AM PST, ID: 20599914

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
01.07.2008 at 07:47AM PST, ID: 20600284

Rank: Sage

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
01.05.2008 at 05:31PM PST, ID: 20592103
You don't say want you want to write in the file, text or bytes. However, you need to use the System.IO.File namespace. This link provides some examples:

http://www.dotnetspider.com/namespace/ShowClass.aspx?ClassId=44
 
01.05.2008 at 11:33PM PST, ID: 20593086
I have attached my code but I don't know how to execute it and how to add information to it?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
using System;
using System.IO;
using System.Text;
 
class CreateFile
{
    public static void Main1()
    {
        string path = @"c:\B2B\Partner.txt";
 
        try
        {
 
            // Delete the file if it exists.
            if (File.Exists(path))
            {
                // Note that no lock is put on the
                // file and the possibliity exists
                // that another process could do
                // something with it between
                // the calls to Exists and Delete.
                File.Delete(path);
            }
 
            // Create the file.
            using (FileStream fs = File.Create(path))
            {
                Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
                // Add some information to the file.
                fs.Write(info, 0, info.Length);
            }
 
            // Open the stream and read it back.
            using (StreamReader sr = File.OpenText(path))
            {
                string s = "";
                while ((s = sr.ReadLine()) != null)
                {
                    Console.WriteLine(s);
                }
            }
        }
 
        catch (Exception Ex)
        {
            Console.WriteLine(Ex.ToString());
        }
    }
}
Open in New Window
 
01.06.2008 at 02:20AM PST, ID: 20593399
The entry point for a console application must be called Main i.e:

  static void Main(string[] args)
  {

You can then pass the string you want to write to the file as a command line arg.
You can set the entry function for the console app and the debugging command line args in the project settings in Visual Studio. Use speech marks " to surround a string in the command line aruments.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
 
namespace ConsoleApplication1
{
    class CreateFile
    {
        static void Main(string[] args)
        {
            string path = @"c:\B2B\Partner.txt";
 
            try
            {
                // Delete the file if it exists.
                if (File.Exists(path))
                {
                    // Note that no lock is put on the
                    // file and the possibliity exists
                    // that another process could do
                    // something with it between
                    // the calls to Exists and Delete.
                    File.Delete(path);
                }
 
                // Create the file.
                using (FileStream fs = File.Create(path))
                {
                    Byte[] info = new UTF8Encoding(true).GetBytes(args[0]);
                    // Add some information to the file.
                    fs.Write(info, 0, info.Length);
                }
 
                // Open the stream and read it back.
                using (StreamReader sr = File.OpenText(path))
                {
                    string s = "";
                    while ((s = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(s);
                    }
                }
            }
 
            catch (Exception Ex)
            {
                Console.WriteLine(Ex.ToString());
            }
        }
    }
}
Open in New Window
 
01.06.2008 at 10:11AM PST, ID: 20594834

Rank: Sage

Hi sfecto;

If I understand you correctly you want to create a program which can be used in other programs to do File IO. In that case you want to create a DLL. Here are the steps to do it and some sample code. Note that I used static functions in the DLL so you do note need to create an instance of the class. I also used the out parameter in the function to pass a message or data back to the caller.

Create an empty solution
1 Create an empty solution in Visual Studio by doing the following
   File->New Project
2. Under project type select "Other Project Types" and then select
   "Visual Studio Solutions"
3. On the Templates side of the dialog select "Blank Solution",
   give it a name and click on OK

Create a project for the DLL
4. Create a "Class Library" solution within the solution created above
   by doing the following
   Open Solution Exploter Window and right click on the solution name.
   Select "Add->New Project" from the pop up menu.
5. Select the Visual C# -> Windows on the Project type section and then
   selecting "Class Library" on the Template side and name the project
   and click OK.
6. Create your DLL to create and get file data - see sample code -

Create a Test program to test the DLL
7. "File->Add->New Project"
8. Select the Visual C# -> Windows on the Project type section and then
   selecting "Console Application" on the Template side and name the project
   and click OK.
9. Add reference to the above DLL in this project
10. Write the Test program to test the DLL - See sample -
11. Test DLL functions.

Before running the project go to the Solution Explorer window and right
click on the Test program solution and select "Set as startup project".

At this point you have two solutions in this project, the DLL and the Test
program.

Fernando
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
// The DLL/Class Library
 
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
 
namespace DLLFileFunctions
{
    public class FileFunctions
    {
        public static bool WriteData(String path, String data, out String msg)
        {
            msg = String.Empty;
            if (!Directory.Exists(Path.GetDirectoryName(path)))
            {
                // Check to see if directory exist
                msg = "Directory does not exist, create before calling this function";
                return false;
            }
 
            // Create the file if it exist it will be deleted first
            using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    // Add some information to the file.
                    sw.Write(data);
                }
            }
 
            // Open the stream and read it back.
            using (StreamReader sr = File.OpenText(path))
            {
                string s = "";
                while ((s = sr.ReadLine()) != null)
                {
                    Console.WriteLine(s);
                }
            }
            return true;
        }
 
        public static bool GetData(String path, out string data)
        {
            // Check if the file exist
            if (!File.Exists(path))
            {
                data = "File does not exist.";
                return false;
            }
 
            // Get the data from the file.
            using (StreamReader sr = new StreamReader(path))
            {
                data = sr.ReadToEnd();
            }
            return true;
        }
    }
}
 
===================================================================
 
// Test program
 
using System;
using System.Collections.Generic;
using System.Text;
using DLLFileFunctions;  // This is the namespace name used in my DLL, Don't forget to add the reference to this project to the DLL.
 
namespace TestForDLL
{
    class Program
    {
        static void Main(string[] args)
        {
            String msg = String.Empty;
            if (!FileFunctions.WriteData(@"C:\Temp\BBB.txt", "Data To Write", out msg))
            {
                Console.WriteLine("Error: " + msg);
                Console.Read();
            }
 
            String data = String.Empty;
 
            if (!FileFunctions.GetData(@"C:\Temp\BBB.txt", out data))
            {
                Console.WriteLine("Error: " + msg);
                Console.Read();
            }
 
            Console.Read();
        }
    }
}
Open in New Window
 
01.06.2008 at 11:42AM PST, ID: 20595270
FernandoSoto
I have done everything you mentioned above .
Its doing everything its supposed to do like deleting the existing the existing file creating new file
but in the text file it says "Data to write". But not the actual data that I typed in the console window.
Thanks
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
//Console Application
// Test program
 
using System;
using System.Collections.Generic;
using System.Text;
using DLLFileFunctions; // This is the namespace name used in my DLL, Don't forget to add the reference to this project to the DLL.
 
 
namespace TestForDLL
{
    class Program
    {
        static void Main(string[] args)
        {
            String msg = String.Empty;
            if (!FileFunctions.WriteData(@"C:\Temp\BBB.txt", "Data To Write", out msg))
            {
                Console.WriteLine("Error: " + msg);
                Console.Read();
            }
 
            String data = String.Empty;
 
            if (!FileFunctions.GetData(@"C:\Temp\BBB.txt", out data))
            {
                Console.WriteLine("Error: " + msg);
                Console.Read();
            }
 
            Console.Read();
        }
    }
}
 
//DLL Code
// The DLL/Class Library
 
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
 
namespace DLLFileFunctions
{
    public class FileFunctions
    {
        public static bool WriteData(String path, String data, out String msg)
        {
            msg = String.Empty;
            if (!Directory.Exists(Path.GetDirectoryName(path)))
            {
                // Check to see if directory exist
                msg = "Directory does not exist, create before calling this function";
                return false;
            }
 
            // Create the file if it exist it will be deleted first
            using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    // Add some information to the file.
                    sw.Write(data);
                }
            }
 
            // Open the stream and read it back.
            using (StreamReader sr = File.OpenText(path))
            {
                string s = "";
                while ((s = sr.ReadLine()) != null)
                {
                    Console.WriteLine(s);
                }
            }
            return true;
        }
 
        public static bool GetData(String path, out string data)
        {
            // Check if the file exist
            if (!File.Exists(path))
            {
                data = "File does not exist.";
                return false;
            }
 
            // Get the data from the file.
            using (StreamReader sr = new StreamReader(path))
            {
                data = sr.ReadToEnd();
            }
            return true;
        }
    }
}
Open in New Window
 
01.06.2008 at 12:29PM PST, ID: 20595473
Replace "Data To Write" on line 17 with args[0]
 
01.06.2008 at 12:39PM PST, ID: 20595503
It gives the following error:

 "Index was outside the bounds of the array"
 
01.06.2008 at 12:43PM PST, ID: 20595514

Rank: Sage

Hi sfecto;

The code provided was just a sample.  Therefore this line of code :

if (!FileFunctions.WriteData(@"C:\Temp\BBB.txt", "Data To Write", out msg))

Which calls the dll and gives the filename and data to write to the function WriteData. You need to change the parameters of the function to accomplish what you need.

Please see sample code to show you how to implement a test program to test the dll.

Also I made two changes to the dll. One was to delete the write to the console code from within the WriteData function and the other was I selected the wrong mode for FileStream options.
Complete sample code below.

Fernando
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
Test Program:
 
using System;
using System.Collections.Generic;
using System.Text;
using DLLFileFunctions;  // This is the namespace name used in my DLL, Don't forget to add the reference to this project to the DLL.
 
namespace TestForDLL
{
    class Program
    {
        static void Main(string[] args)
        {
            String msg = String.Empty;
            String path = String.Empty;
            String saveData = String.Empty;
            String getData = String.Empty;
 
            // Enter the path an name of the file to be created 
            Console.Write("Enter path and filename > ");
            path = Console.ReadLine();
 
            // Input data from the console to be stored in a file
            Console.WriteLine("Enter Data > ");
            saveData = Console.ReadLine();
            Console.WriteLine("\n");
 
            // Save the data to the file
            if (!FileFunctions.WriteData(path, saveData, out msg))
            {
                Console.WriteLine("Error: " + msg);
                Console.Read();
            }
 
            // Read the data from the file.
            if (!FileFunctions.GetData(path, out getData))
            {
                Console.WriteLine("Error: " + msg);
                Console.Read();
            }
 
            Console.WriteLine(getData);
            Console.Write("Press any key to teminate program");
            Console.Read();
        }
    }
}
 
==================================================================
 
// DLL
 
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
 
namespace DLLFileFunctions
{
    public class FileFunctions
    {
        public static bool WriteData(String path, String data, out String msg)
        {
            msg = String.Empty;
            if (!Directory.Exists(Path.GetDirectoryName(path)))
            {
                // Check to see if directory exist
                msg = "Directory does not exist, create before calling this function";
                return false;
            }
 
            // Create the file if it exist it will be deleted first
            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    // Add some information to the file.
                    sw.Write(data);
                }
            }
            return true;
        }
 
        public static bool GetData(String path, out string data)
        {
            // Check if the file exist
            if (!File.Exists(path))
            {
                data = "File does not exist.";
                return false;
            }
 
            // Get the data from the file.
            using (StreamReader sr = new StreamReader(path))
            {
                data = sr.ReadToEnd();
            }
            return true;
        }
    }
}
Open in New Window
 
01.06.2008 at 12:45PM PST, ID: 20595522
You'r not passing any command line arguments. Do either:
1. <app>.exe "Text to wite"
2. Add "Text to Write" in the command line arument in the project setting. Debugging tab.
 
01.06.2008 at 01:25PM PST, ID: 20595676

Rank: Sage

Hi sfecto;

If you wish to enter the data from the programs command line at the dos prompt you can use this test program. Please read comments in code on how to pass the parameters into the program. Also in Visual Studio .Net you can set the command line arguments in the projects property page so that you do not have to go to the dos prompt. To do so go to the solution explorer window and right click on the Test programs name and select Properties from the pop up menu. On the property page on the left click on "Debug" then on the right in the text box marked Command Line Argument enter the arguments.

Fernando
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
// Test Program
 
using System;
using System.Collections.Generic;
using System.Text;
using DLLFileFunctions;  // This is the namespace name used in my DLL, Don't forget to add the reference to this project to the DLL.
 
namespace TestForDLL
{
    class Program
    {
        static void Main(string[] args)
        {
            String msg = String.Empty;
            String path = String.Empty;
            String saveData = String.Empty;
            String getData = String.Empty;
 
            if (Environment.GetCommandLineArgs().Length != 3)
            {
                // You need two arguments to be passed in and must be in
                // the format of programName "/f:Path&Filename" "/d:The data to be save" or
                // the format of programName  "/d:The data to be save" "/f:Path&Filename"
                Console.WriteLine("Invalid Number of Argument...");
                Console.Read();
                return;
            }
            else
            {
                String[] cmArgs = Environment.GetCommandLineArgs();
                for (int idx = 1; idx < 3; idx++)
                {
                    if (cmArgs[idx].Substring(0, 3) == "/p:")
                    {
                        // Get the file name to write to
                        path = cmArgs[idx].Substring(3);
                    }
                    else
                    {
                        if (cmArgs[idx].Substring(0, 3) == "/d:")
                        {
                            // get the data to write to the file
                            saveData = cmArgs[idx].Substring(3);
                        }
                        else
                        {
                            // Error in argument list
                            Console.WriteLine("Invalid Argument..." + 
                                cmArgs[idx].Substring(0, 3));
                            Console.Read();
                            return;
                        }
                    }
                }
            }
 
            // Save the data to the file
            if (!FileFunctions.WriteData(path, saveData, out msg))
            {
                Console.WriteLine("Error: " + msg);
                Console.Read();
            }
 
            // Read the data from the file.
            if (!FileFunctions.GetData(path, out getData))
            {
                Console.WriteLine("Error: " + msg);
                Console.Read();
            }
 
            Console.WriteLine(getData);
            Console.Write("Press any key to teminate program");
            Console.Read();
        }
    }
}
Open in New Window
 
01.06.2008 at 05:34PM PST, ID: 20596543
FernandoSoto:
The following code from morning is working fine from console. How can I type multiple lines in console without terminating my program. The following desired format:

1234   4567
1011   2101
6212   1544

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
using System;
using System.Collections.Generic;
using System.Text;
using DLLFileFunctions;  // This is the namespace name used in my DLL, Don't forget to add the reference to this project to the DLL.
 
namespace TestForDLL
{
    class Program
    {
        static void Main(string[] args)
        {
            String msg = String.Empty;
            String path = String.Empty;
            String saveData = String.Empty;
            String getData = String.Empty;
 
            // Enter the path an name of the file to be created 
            Console.Write("Enter path and filename > ");
            path = Console.ReadLine();
 
            // Input data from the console to be stored in a file
            Console.WriteLine("Enter Data > ");
            saveData = Console.ReadLine();
            Console.WriteLine("\n");
 
            // Save the data to the file
            if (!FileFunctions.WriteData(path, saveData, out msg))
            {
                Console.WriteLine("Error: " + msg);
                Console.Read();
            }
 
            // Read the data from the file.
            if (!FileFunctions.GetData(path, out getData))
            {
                Console.WriteLine("Error: " + msg);
                Console.Read();
            }
 
            Console.WriteLine(getData);
            Console.Write("Press any key to teminate program");
            Console.Read();
        }
    }
}
Open in New Window
 
01.06.2008 at 07:23PM PST, ID: 20596937

Rank: Sage

Hi sfecto;

Take the code from morning, shown below, and place it in a control loop, such as a while, for or do and when you enter the loop initialize the variables that you have enter the mailn method. You will also need to change the the line of code in the dll, using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write)), so that FileMode.Create becomes FileMode. Append so that you do not ovwer right the file with the next line of input line that is entered.in the console. With all the info from this above post you should be able to put together what you need.

Fernando

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
            // Enter the path an name of the file to be created 
            Console.Write("Enter path and filename > ");
            path = Console.ReadLine();
 
            // Input data from the console to be stored in a file
            Console.WriteLine("Enter Data > ");
            saveData = Console.ReadLine();
            Console.WriteLine("\n");
 
            // Save the data to the file
            if (!FileFunctions.WriteData(path, saveData, out msg))
            {
                Console.WriteLine("Error: " + msg);
                Console.Read();
            }
 
            // Read the data from the file.
            if (!FileFunctions.GetData(path, out getData))
            {
                Console.WriteLine("Error: " + msg);
                Console.Read();
            }
 
            Console.WriteLine(getData);
            Console.Write("Press any key to teminate program");
            Console.Read();
        }
Open in New Window
 
01.06.2008 at 08:26PM PST, ID: 20597085
FernandoSoto
It creates empty text file.

Sarah
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
//TEST
using System;
using System.Collections.Generic;
using System.Text;
using DLLFileFunctions;  // This is the namespace name used in my DLL, Don't forget to add the reference to this project to the DLL.
 
namespace TestForDLL
{
    class Program
    {
        static void Main(string[] args)
        {
            String msg = String.Empty;
            String path = String.Empty;
            String saveData = String.Empty;
            String getData = String.Empty;
 
            // Enter the path an name of the file to be created 
            Console.Write("Enter path and filename > ");
            path = Console.ReadLine();
 
            // Input data from the console to be stored in a file
            Console.WriteLine("Enter Data > ");                
            
            saveData = Console.ReadLine();
            while (saveData.Length > 0)
            {
            saveData = Console.ReadLine();             
            }
            
            Console.WriteLine("\n");
 
            // Save the data to the file
            if (!FileFunctions.WriteData(path, saveData, out msg))
            {
                Console.WriteLine("Error: " + msg);
                Console.Read();
            }
 
            // Read the data from the file.
            if (!FileFunctions.GetData(path, out getData))
            {
                Console.WriteLine("Error: " + msg);
                Console.Read();
            }
 
            Console.WriteLine(getData);
            Console.Write("Press any key to teminate program");
            Console.Read();
        }
    }
}
 
 
 
 
// DLL
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
 
namespace DLLFileFunctions
{
    public class FileFunctions
    {
        public static bool WriteData(String path, String data, out String msg)
        {
            msg = String.Empty;
            if (!Directory.Exists(Path.GetDirectoryName(path)))
            {
                // Check to see if directory exist
                msg = "Directory does not exist, create before calling this function";
                return false;
            }
 
            // Create the file if it exist it will be deleted first
        //  using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
          using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    // Add some information to the file.
                    sw.Write(data);
                }
            }
            return true;
        }
 
        public static bool GetData(String path, out string data)
        {
            // Check if the file exist
            if (!File.Exists(path))
            {
                data = "File does not exist.";
                return false;
            }
 
            // Get the data from the file.
            using (StreamReader sr = new StreamReader(path))
            {
                data = sr.ReadToEnd();
            }
            return true;
        }
    }
}
Open in New Window
 
01.06.2008 at 08:54PM PST, ID: 20597141

Rank: Sage

The only way I can see that is if on this first line of code

saveData = Console.ReadLine();

Which gets the data from the console. This causes saveData to have data in the variable and therefore causes the if statement to execute its block and if you hit enter without typing anything in saveData will be the empty string.

So in other words this is what may be happening.

saveData = Console.ReadLine();  // First line lets say you enter Hello World
saveData now contains Hello World then when you enter the return, key then
saveData has the value of empty string.

Fernando
 
01.06.2008 at 09:20PM PST, ID: 20597185
FernandoSoto
Its saving everything in one line in the text file, though I am typing it in multiple line in Console.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
using System;
using System.Collections.Generic;
using System.Text;
using DLLFileFunctions;  // This is the namespace name used in my DLL, Don't forget to add the reference to this project to the DLL.
 
namespace TestForDLL
{
    class Program
    {
        static void Main(string[] args)
        {
            String msg = String.Empty;
            String path = String.Empty;
            String saveData = String.Empty;
            String getData = String.Empty;
 
            // Enter the path an name of the file to be created 
            Console.Write("Enter path and filename > ");
            path = Console.ReadLine();
 
            // Input data from the console to be stored in a file
            Console.WriteLine("Enter Data > ");                
            
            saveData = Console.ReadLine();          
            
            Console.WriteLine("\n");
 
            // Save the data to the file
            if (!FileFunctions.WriteData(path, saveData, out msg))
            {
                Console.WriteLine("Error: " + msg);
                Console.Read();
            }                 
 
            while (saveData.Length > 0)
            {
                saveData = Console.ReadLine();
                if (!FileFunctions.WriteData(path, saveData, out msg))
                {
                    Console.WriteLine("Error: " + msg);
                    Console.Read();
                }             
            }
            // Read the data from the file.
            if (!FileFunctions.GetData(path, out getData))
            {
                Console.WriteLine("Error: " + msg);
                Console.Read();
            }      
            Console.WriteLine(getData);
            Console.Write("Press any key to teminate program");
            Console.Read();
        }
    }
}
Open in New Window
 
01.07.2008 at 07:03AM PST, ID: 20599914
FernandoSoto:
I am sorry, I am bit new to this..............
Its working fine for the most part except that its saving  everything on ONE Line rather than saving it on multiple lines.

Thanks for ur help
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
//Test Program
using System;
using System.Collections.Generic;
using System.Text;
using DLLFileFunctions;  // This is the namespace name used in my DLL, Don't forget to add the reference to this project to the DLL.
 
namespace TestForDLL
{
    class Program
    {
        static void Main(string[] args)
        {
            String msg = String.Empty;
            String path = String.Empty;
            String saveData = String.Empty;
            String getData = String.Empty;
 
            // Enter the path an name of the file to be created 
            Console.Write("Enter path and filename > ");
            path = Console.ReadLine();
 
            // Input data from the console to be stored in a file
            Console.WriteLine("Enter Data > ");                
            
            saveData = Console.ReadLine();          
            
            Console.WriteLine("\n");
 
            // Save the data to the file
            if (!FileFunctions.WriteData(path, saveData, out msg))
            {
                Console.WriteLine("Error: " + msg);
                Console.Read();
            }                 
 
            while (saveData.Length > 0)
            {
                saveData = Console.ReadLine();
                if (!FileFunctions.WriteData(path, saveData, out msg))
                {
                    Console.WriteLine("Error: " + msg);
                    Console.Read();
                }             
            }
            // Read the data from the file.
            if (!FileFunctions.GetData(path, out getData))
            {
                Console.WriteLine("Error: " + msg);
                Console.Read();
            }      
            Console.WriteLine(getData);
            Console.Write("Press any key to teminate program");
            Console.Read();
        }
    }
}
//DLL
// DLL
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
 
namespace DLLFileFunctions
{
    public class FileFunctions
    {
        public static bool WriteData(String path, String data, out String msg)
        {
            msg = String.Empty;
            if (!Directory.Exists(Path.GetDirectoryName(path)))
            {
                // Check to see if directory exist
                msg = "Directory does not exist, create before calling this function";
                return false;
            }
 
            // Create the file if it exist it will be deleted first
        //  using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
          using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    // Add some information to the file.
                    sw.Write(data);
                }
            }
            return true;
        }
 
        public static bool GetData(String path, out string data)
        {
            // Check if the file exist
            if (!File.Exists(path))
            {