Mihir
Mihir

Reputation: 8734

while trying to write data into text file it is showing server doesnot exist in this context

Here i have code base like this:

class Program
{
    static void Main(string[] args)
    {
        try
        {
            'String str;
            'str = Server.MapPath("/financila_csharp");


            StreamReader reader = new StreamReader("selectedmdx.txt");
            StreamWriter writer = new StreamWriter("selectedxmlmdx.txt");
            string line = reader.ReadLine();
            while (line != null)
            {
                XmlDocument dom = new XmlDocument();
                dom.LoadXml("<Result>" + System.Security.SecurityElement.Escape(line) + "</Result>");
                writer.WriteLine(dom.DocumentElement.OuterXml);
                line = reader.ReadLine();
            }

            Console.WriteLine("Completed");
            reader.Close();
            writer.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);    
        }
        Console.ReadLine();
    }
}

In console window it is showing "specified file does not exist", even if I have the "selectedmdx.txt" file in the same project directory.

How can I fix it?

Upvotes: 1

Views: 268

Answers (3)

vamyip
vamyip

Reputation: 1171

Is the file "selectedmdx.txt" added to your visual studio solution? If yes, select it in solution explore and press F4. In the properties window, set the "Copy to output directory" to 'Copy always' or 'Copy if newer', whichever is more suitable for you. This will copy the file to the output directory from where your code is actually running.

Hope this helps...

Vamyip

Upvotes: 0

PawanS
PawanS

Reputation: 7193

I think this is a non web application. so try

 str = System.IO.Path.GetFullPath("/financila_csharp");

it will work perfectly

Upvotes: 2

Phil Carson
Phil Carson

Reputation: 884

Server.MapPath works with web applications not console applications.

Refer to this question for getting the path in a console application.

Upvotes: 0

Related Questions