babno
babno

Reputation: 309

The differences of running on a server

I have a program that fetches a file from a server and downloads it. It is itself located on the server, so to run it you open your browser and type in a URL with parameters.

Now, it runs perfectly fine when the program is located on my machine. However when accessed via your browser it seems to hang. I have tried MessageBox.Show(username, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);

string[] lines = {"First line", "Second line", "Third line"};
        System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);

and both of those cause errors when done on the server (but it's fine when on my own machine).

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt"))
file.WriteLine("asfsd");

this seems to hang on the writeline, as the file is created but no content is ever put into the file. Any idea why this is happening or suggestions of things I can put so I know where the problem is?

Upvotes: 1

Views: 117

Answers (3)

Chris Kooken
Chris Kooken

Reputation: 33880

If I had to guess. Id say you didnt have permissions. Try wrapping everything in an Impersonate statement or give the worker process access to the directory.

using (new Impersonator(username, domain,password)) {
      using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt"))
      file.WriteLine("asfsd");
      }
 }

Also, make sure the directory exists before writing.

Upvotes: 4

walther
walther

Reputation: 13600

You're trying to write to a local file and IIS on the server obviously doesn't have permissions to do that (what a surprise :-) ). Save files in your webapp folder instead, not in C:\

Upvotes: 1

Jaime Torres
Jaime Torres

Reputation: 10515

It sounds like you're mixing WinForms development with ASP.NET development, and they are two very different animals.

If you are having an issue writing to a file, it's more than likely caused by a permissions issue. IIS default app pool typically runs as NETWORK SERVICE user, which most likely doesn't have permissions in your target directory.

Upvotes: 2

Related Questions