1

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?

Mihir
  • 8,696
  • 18
  • 56
  • 87
  • As the executable executing from Bin\Debug or Bin\Release folder, you have to specify the path like that. Try using "../../selectedmdx.txt". But its not a valid fix. You can use Assembly.GetExecutingAssembly().Location for finding the executable location and resolve path using the location. – Anuraj Feb 03 '11 at 07:59
  • @All, i just modified my code..i removed server.map(), but it is showing specified file doesnot exist....any idea please? – Mihir Feb 03 '11 at 08:33
  • @Mihir: Did you tried my comment? – Anuraj Feb 03 '11 at 08:44
  • @Anuraj...actually your comment is not understand by me...i am novice user..sorry to say this – Mihir Feb 03 '11 at 08:45
  • @All..finally i found the answer for my self.. u just place the full path just like C://Users\ymihir/Desktop/Mihir/vbmine/financila_csharp/financila_csharp/selectedxmlmdx.txt. then out put came successfully.. happy to say this..thanku one and all for trying..thanks for your co-operation.. :) – Mihir Feb 03 '11 at 08:50
  • @Mihir This won't solve your problem if you will have to move your web to some other location. – vamyip Feb 03 '11 at 09:28
  • @vamyip...may be..but i am at initial state...so by now it is better i think.. :) – Mihir Feb 03 '11 at 11:57

3 Answers3

2

I think this is a non web application. so try

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

it will work perfectly

PawanS
  • 7,033
  • 14
  • 43
  • 71
  • @Pawan...thank u. but i removed server.map() and trying to execute..but it is still giving message as file does not exist..any idea? – Mihir Feb 03 '11 at 08:35
  • first check if file is existing or not. If file is already existing then no need to create the file, just append or modify. And if it is not exixting then you create it.... I will edit the post. – PawanS Feb 03 '11 at 08:42
  • The above statement is also not needed for this program.. thanku – Mihir Feb 03 '11 at 08:51
  • @mihir... ok that depends on your requirement. I just said IO.Path.GetFullPath(string str) is oppose on server.MapPath(string str) for windows application. – PawanS Feb 03 '11 at 09:41
0

Server.MapPath works with web applications not console applications.

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

Community
  • 1
  • 1
Phil Carson
  • 884
  • 8
  • 18
  • @Phil..yes i referred in google. thank u. but i changed my code..can u please find the solution..! it is showing file doesnt exists. thank u – Mihir Feb 03 '11 at 08:41
0

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

vamyip
  • 1,161
  • 1
  • 10
  • 35
  • @Vamyip..thanku so much..but i am unable to find the that copy to output directory..i am novice user plz tell me. i clicked on solution explorer and press F4 but in properties there is no such option..did i done any thing wrong? – Mihir Feb 03 '11 at 08:39
  • First check whether the file "selectedmdx.txt" is added to your solution. If yes, then select that file inside solution explorer before pressing F4. – vamyip Feb 03 '11 at 09:25