I am getting error while moving file to NAS path - c#

I have a web application to upload file. Application first store file to local server and then move to NAS path. Application is using System.IO.File.Move method of C# to move file from local server to NAS.
I am seeing strange issue. After login to application first upload works fine. but if upload another file immediately,it fails. If I wait for 3-5 minutes after a file upload and try to upload file again. it works.
I have tested the connectivity from local server to NAS, it is fine.
Same web application was running on another server, we have stopped application on that server and moved to this server. This issue is happening only on new server not on old server.
Error message:The specified network name is no longer available.
Line of code which throw error: File.Move(sourceFilePath,DestinationFilePath)
Also I was trying to see if connection to NAS path has issue. So what I did is that just before the line of code which is throwing error I added a condition:
If(!File.Exist(DestinationFilePath)){ throw new Exception(<some message>)
After adding this check application is working fine. I am not sure how?

Related

Unable to determine if a file is on a web server because the various methods of determining the directory do not work

I am developing an application in asp.net, vs2015 using c# and the development environment is a Win10Pro machine. I can use any of the various methods to obtain the working directory and see if a particular file exists on the dev pc, but not on the Web Server. I have tried the methods laid out on:
Get current application physical path within Application_Start
All work on the Dev PC, but when used on the Web Server it will not return the working directory. The Server is a 2016 Data server using IIS10. The issue is that the web site I am putting together work fine, except to display GrapeCity ActiveReports reports AR15. The web page containing their web viewer opens just fine and is looking for a report file (MyReport.rdlx). The global.aspx file is pointing to the root directory but when the web viewer opens up, it says File Not Found. I have absolutely no idea and tech support is not sure. Is this an IIS issue that is preventing the code to locate and verify the file is there? Any direction would be much appreciated. This has been very frustrating and time consuming.
AppDomain.CurrentDomain.BaseDirectory does not work, HttpRuntime.AppDomainAppPath does not as well as all the others. The request comes back blank.
string filename = AppDomain.CurrentDomain.BaseDirectory.ToString() +"SPU01_Dates.rdlx";
if (File.Exists(filename))
{
Response.Write("YES");
}
else
{
Response.Write("NO");
Response.Write("</br");
Response.Write(filename);
}
All this just returns nothing.
Thanks.
Try this code
if (File.Exists(Server.MapPath(filename)))
Check if a file exists on the server
In my test, it returned YES and worked well. Did you put "SPU01_Dates.rdlx" file in root folder?
In the development environment, it returned YES, and when I deployed it to IIS, it returned NO. I found that during the deployment process, the rdlx file was not deployed with the project, so I recreated one in the deployed folder, and it returned YES.
The test proves that AppDomain.CurrentDomain.BaseDirectory is the most accurate way to get the file path. When you test this code in IIS, does it return NO or empty? Returning empty means that this piece of code has not been executed.

Server.Mappath() fail on remote server

i use like this codes for prepare paths.
string path = Server.MapPath("~/"+ User.Identity.Name+ "/file.zip");
i take this error when i run this code on localhost
Index:582 Not allowed to load local resource: file:///C:/Users/trkmml/Documents/GitHub/OfisTakip/trkmml/file.zip
i think its not problem (its chrome security thing). anyway file path is correct.
On the other hand, when I upload the program to hosting, it adds "file:///C:/Inetpub/vhosts" to the front of the link. how can i cancel this part. how do I make it just
http://example.com/httpdocs/trkmml/file.zip".
here is error on remote host
Not allowed to load local resource: file:///C:/Inetpub/vhosts/example.com/httpdocs/trkmml/file.zip

Failed to load resources

I have application which I deploy to Azure and suddenly I catch one error which wasnt in my local machine when I tested application.
Failed to load resource: the server responded with a status of 404 (Not Found)
SO when I tested application in my local machine everything works perfect without any errors, and when I move to test application live on server many option doesnt work, and in console manager I get this kind of error.
Any help, what can be problem here ?
404 is resource not found error. Most probable reasons are your files are in “../“ folder and you are trying to access file in “../..” folder.
I would suggest using URLs like /Folder/subfolder instead of relative URLs like “../parentfolder/subfolder”..
Also, its good to use “~”..
More on paths here:
https://msdn.microsoft.com/en-us/library/ms178116.aspx

Can't open databse requested by login, login failed, login failed for user

I have developed c# WPF application, I am using MDF file for database,
my connection string looks like :
Data
Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\appDataBase.mdf;Integrated
Security=True;Connect Timeout=30
here I am replacing |DataDirectory| with user's appdata folder.
the database file is in user's appdata folder, I created a setup which
copies .mdf file to _users appdata folder when I install it in
client's computer, it works perfectly
but when I uninstalls and
reinstall it, I am not able to connect that file, getting the error:
Can't open database
"C:/users/myusername/appdata/roaming/myapplication/database/appDataBase.mdf"
requested by login, login failed, login failed for user..
File is there, but somehow it is used by SQL server and can't
open it. also while starting application every time I am creating
backup of that file, that one also failing with error
System.IO.IOException: The process cannot access the file
'C:\Users\myusername\AppData\Roaming\myapplication\Database\appDataBase.mdf'
because it is being used by another process.
Can anybody give me exact solution, because i have to use localdb only on my client computer , i don't want them let to do any extra configuration for installation of my app, i.am still facing iasue after i changed my code to close db connection on main window closing event.
This type of things happens because one of your object which is using that database is still running in the background so please dispose all the database related instance also dispose and close all the database related object in catch().
Try catch will help you to detect exact line number where you are getting error and also show due to which object you are getting error.
If you want to check why this is happening then open task manager and look out for services there you definitely find service which is holding object for database.

Upload File from Client to Server C#

Got an issue "Path Not Found" when try to upload an image from Client site to another server(Not the application server). The code is just like below:
FileUpload1.SaveAs(Server.MapPath(#"xx.xx.xx.xx\" + FileUpload1.FileName));
The error occurred after i have deployed the application into the web application server. But when run locally successfully uploaded.
I suspect the File Uploader that run from the application server cannot found the path from client site. It is because the path of the image does not existed in the application server.
There are two question regarding this issue:
1) Is there anyway to resolved this issue.
2) Is there any differences between "FileUpload1.PostedFile.SaveAs()" and FileUpload1.SaveAs()?
Thanks for trying to help me on this.
You should write path as img.Save("c:\\Users\\user\\Desktop\\Barcodes\\1.png", System.Drawing.Imaging.ImageFormat.Png);

Categories