Copy file to remote location throws network path not found intermittently - c#

I am trying to copy a file generated (Excel file) by my c# code into a remote network path to which I have access as below:
string folder = "\\\\testing-path\\Audit\\Reports";
if (!(Directory.Exists(folder + "\\" + DateTime.Now.ToString("MM-dd-yyyy") + "\\" + "Audit")))
{
Directory.CreateDirectory(folder + "\\" + DateTime.Now.ToString("MM-dd-yyyy") + "\\" + "Audit");
}
folder = folder + "\\" + DateTime.Now.ToString("MM-dd-yyyy") + "\\" + "Audit";
if (File.Exists(folder + "\\Audit- " + fname + ".xlsx"))
{
File.Delete(folder + "\\Audit- " + fname + ".xlsx");
}
string fileName = folder + "\\Audit- " + fname + ".xlsx";
wb.SaveAs(fileName,
Excel.XlFileFormat.xlWorkbookDefault, null, null,
false, false, Excel.XlSaveAsAccessMode.xlNoChange,
null, null, null, null, null);
This code works perfectly fine 8/10 times and throws network error (Network path not found) other 2 times. When the remote path throws this error I open the above remote path using run command on the machine I run this code and I am able to access it as normal. Closing the folder opened manually and re-running the code is solving the issue. What could be the issue? Am I doing something wrong here?
PS: I also tried to save the excel file onto desktop and then use File.Copy and there is no change to this intermittent behaviour.

Check the following things
1) First you have to check whether machine is on or off
2) Then check you have access rights to put the file in that particular folder and dont
put it inside ' C ' Drive because C is not accessible for other users in network put it inside D or E drive.
3) If Folder is Missing You have to Create A Folder first
and what is ' WB '
if(!Directory.Exists("\\\\testing-path\\Audit\\Reports"))
Directory.Create("\\\\testing-path\\Audit\\Reports");
Use
FilePath Byte
File.ReadAllBytes("FilePath/FileName.Extension",byte) // D:\\Test.xls,12878
then in Remote Location
FilePath Byte
File.WriteAllBytes("FilePath/FileName.Extension",byte)// D:\\Test123.xls,12878
Check this Link
Accessing paths in Remote Machine : http://www.codeproject.com/Questions/184633/Connect-to-a-shared-folder-using-ip-address-in-vb6

Related

File.Copy from local to shared folder in server

I'm having problems when copying a .txt from a local server ("D:\AuditFiles") to a shared folder in another server ("\\PrintServer\SharedFolder"). It throws the exception:
"The filename, directory name, or volume label syntax is incorrect."
I thought it could be something with the path format, so I tried by adding to the server path an #:
#Configuration.Manager["Path"] | #"\\ServerPath\SharedFolder"
I've also tried with this format: \\ServerPath\SharedFolder... None of them worked.
By the way its not an access problem, cause i've tried to do the same thing running a command prompt from within c#:
System.Diagnostics.Process.Start("cmd.exe", "/C COPY PATH1, PATH2"); //This worked and copied the file.
I'd be greatful if someone could give me a clue of what could be the problem here. At least an advice of what to do.
Thanks in advance and sorry me bad english!
Edit:
This is the part of the code that should work:
string pathPrevDay = "D:\AuditFiles\enc_" + svr.Name + "_counts" + day.AddDays(-1).ToString("dd-MM-yy") + ".txt";
if(File.Exists(pathPrevDay))
{
File.Copy(pathPrevDay, #ConfigurationManager.AppSettings["MAIL_SERVER_PATH"]);
}
You need to escape the backslashes and indicate the name of the file in File.Copy(pathPrevDay, ConfigurationManager.AppSettings["MAIL_SERVER_PATH"]);.
Change this:
string pathPrevDay = "D:\AuditFiles\enc_" + svr.Name + "_counts" + day.AddDays(-1).ToString("dd-MM-yy") + ".txt";
to this:
string pathPrevDay = "D:\\AuditFiles\\enc_" + svr.Name + "_counts" + day.AddDays(-1).ToString("dd-MM-yy") + ".txt";
You can also use single slashes (/) instead like this:
string pathPrevDay = "D:/AuditFiles/enc_" + svr.Name + "_counts" + day.AddDays(-1).ToString("dd-MM-yy") + ".txt";

Access to the path * is denied

I have two WebServices that create files at the same director using the following method:
var schemaDir = _propClass.RepositorySettingRoot + #"\" + webServiceId;
if (!Directory.Exists(schemaDir))
{
Directory.CreateDirectory(schemaDir);
}
var schemaFile = schemaDir + #"\" + webMethodId + ".txt";
File.WriteAllText(schemaFile, webMethodSchema);
When trying to delete the file using File.Delete, if it was created by the 1st service it's deleted properly, but if it was created using the second, the exception Access to the path * is denied is raised.
The deletion code
schemaDir = _propClass.RepositorySettingRoot + #"\" + webServiceId + #"\" + webMethodId + ".txt";
if (File.Exists(schemaDir))
{
File.Delete(schemaDir);
}
I've found the answer ..
It's a security issue.
The file should be deleted by the service created it.
The deletion method was on ws1, so it can delete only the files it created.

Java (Minecraft) doesnt start from c#

When I copy paste the javaw.exe -arguments to console it works but, when I launch it like this it doesn't work.
string directory = "C:\\Users\\Can\\AppData\\Roaming\\.minecraft";
string java = #"C:\windows\system32\javaw.exe";
string javaLocation = "C:\\Program Files\\Java\\jre7\\bin\\javaw.exe";
string RAM = "1G";
string username = "namehere";
string token = "--session token:"+tokenGenerated;
string version = "1.6.2";
string launch = "-Xmx" + RAM + " -Djava.library.path={0}\\versions\\1.6.2\\1.6.2-natives-7453523379463 -cp {0}\\libraries\\net\\sf\\jopt-simple\\jopt-simple\\4.5\\jopt-simple-4.5.jar;{0}\\libraries\\com\\paulscode\\codecjorbis\\20101023\\codecjorbis-20101023.jar;{0}\\libraries\\com\\paulscode\\codecwav\\20101023\\codecwav-20101023.jar;{0}\\libraries\\com\\paulscode\\libraryjavasound\\20101123\\libraryjavasound-20101123.jar;{0}\\libraries\\com\\paulscode\\librarylwjglopenal\\20100824\\librarylwjglopenal-20100824.jar;{0}\\libraries\\com\\paulscode\\soundsystem\\20120107\\soundsystem-20120107.jar;{0}\\libraries\\argo\\argo\\2.25_fixed\\argo-2.25_fixed.jar;{0}\\libraries\\org\\bouncycastle\\bcprov-jdk15on\\1.47\\bcprov-jdk15on-1.47.jar;{0}\\libraries\\com\\google\\guava\\guava\\14.0\\guava-14.0.jar;{0}\\libraries\\org\\apache\\commons\\commons-lang3\\3.1\\commons-lang3-3.1.jar;{0}\\libraries\\commons-io\\commons-io\\2.4\\commons-io-2.4.jar;{0}\\libraries\\net\\java\\jinput\\jinput\\2.0.5\\jinput-2.0.5.jar;{0}\\libraries\\net\\java\\jutils\\jutils\\1.0.0\\jutils-1.0.0.jar;{0}\\libraries\\com\\google\\code\\gson\\gson\\2.2.2\\gson-2.2.2.jar;{0}\\libraries\\org\\lwjgl\\lwjgl\\lwjgl\\2.9.0\\lwjgl-2.9.0.jar;{0}\\libraries\\org\\lwjgl\\lwjgl\\lwjgl_util\\2.9.0\\lwjgl_util-2.9.0.jar;{0}\\versions\\1.6.2\\1.6.2.jar net.minecraft.client.main.Main --username " + username + " " + token + " --version " + version + " --gameDir {0} --assetsDir {0}\\assets";
launch = String.Format(launch, directory);
string text = launch;
// WriteAllText creates a file, writes the specified string to the file,
// and then closes the file.
Directory.SetCurrentDirectory(#"C:\windows\system32\");
Process.Start("javaw.exe",
Path.Combine(launch));
What am I doing wrong?
Why do you need to call Path.Combine if your whole path is in one string?
Assuming your javaw.exe is actually in C:\windows\system32\, Process.Start("java.exe", launch); should work as intended.
Source - Path on MSDN: http://msdn.microsoft.com/en-us/library/system.io.path.aspx
Just use Java's real location instead of "javaw" like "c:\programfiles\Java\jre7\bin\javaw.exe"

Use Sharepoint Copy web service to copy folder and contents

I am trying to make a program that copies all of a Sharepoint folder's contents (all subfolders and files) into another Sharepoint folder. Both of these folders will be on the same Sharepoint site.
However, I am trying to do this remotely - if possible*. Therefore, I have tried using the Copy web service without success. The Copy web service appears to only work with copying files, not folders. In addition, I cannot determine a way to iterate through the folder's contents to copy everything - it will only copy one item.
Thank you for any insights or tips,
Scott
*From a custom CRM workflow activity
~~Edited for clarification~~
In the end, I decided to create my own custom web service in Sharepoint that I was able to successfully access from Microsoft CRM. If anyone is interested, I've pasted the C# code I used to copy the folder structure:
public String CopyFolderContents(String sourceURL, String folderURL, String destinationURL)
{
try
{
#region Copying Code
//Get the SPSite and SPWeb from the sourceURL
using (SPWeb oWebsite = new SPSite(sourceURL).OpenWeb())
{
//Get the parent folder from the folderURL
SPFolder oFolder = oWebsite.GetFolder(folderURL);
//Create a list of all files (not folders) on the current level
SPFileCollection collFile = oFolder.Files;
//Copy all files on the current level to the target URL
foreach (SPFile oFile in collFile)
{
oFile.CopyTo(destinationURL + "/" + oFile.Name, true);
}
//Create a list of all folders on the current level
SPFolderCollection collFolder = oFolder.SubFolders;
//Copy each of the folders and all of their contents
String[] folderURLs = new String[collFolder.Count];
int i = 0;
foreach (SPFolder subFolder in collFolder)
{
folderURLs[i++] = subFolder.Url;
}
for (i = 0; i < folderURLs.Length; i++)
{
SPFolder folder = collFolder[folderURLs[i]];
folder.CopyTo(destinationURL + "/" + folder.Name);
}
}
#endregion Copying Code
}
catch (Exception e)
{
#region Exception Handling
String Message;
if (e.InnerException != null)
Message = "MESSAGE: " + e.Message + "\n\n" +
"INNER EXCEPTION: " + e.InnerException.Message + "\n\n" +
"STACK TRACE: " + e.StackTrace + "\n\n" +
"Source: " + sourceURL + "\n" +
"Folder: " + folderURL + "\n" +
"Destination: " + destinationURL;
else
Message = "MESSAGE: " + e.Message + "\n\n" +
"STACK TRACE: " + e.StackTrace + "\n\n" +
"Source: " + sourceURL + "\n" +
"Folder: " + folderURL + "\n" +
"Destination: " + destinationURL;
throw new Exception(Message);
#endregion Exception Handling
}
return "Operation Successful!";
}
All I did was add this method into a Sharepoint web service and called it from CRM and it works.
Thanks to everyone who provided other answers,
Scott
There is a simple solution to this problem , as its just about copy and paste use the simple XCOPY command
XCOPY Copy files and/or directory trees to another folder. XCOPY is similar to the COPY command except that it has additional switches to specify both the source and destination in detail.
To copy a folder including all subfolders
XCOPY C:\utils\* D:\Backup\utils /s /i
here /i defines the destination as a folder
for more detail please refer this link

Unable to generate file from ASP.NET with inkscape

I have an ASP.NET application on my local machine that works. This application takes an SVG file and creates a PNG from it using inkscape. I have tried to migrate that application to my production server. Oddly, the png creation is not working. The really strange part is, an Exception is not being thrown either. I have taken the command line parameters that are being created and copied and pasted them into the command line environment and they work. For instance, here is the line:
inkscape.exe -f "C:\inetpub\wwwroot\MyTest\sample.svg" -e "C:\inetpub\wwwroot\MyTest\sample.png"
I thought it was something simple, so I extracted the code into a sample web project. This project just converts a .svg to a .png. Once again, it worked in my local environment, but not in the production environment. Here is the code:
protected void executeButton_Click(object sender, EventArgs e)
{
try
{
string sourceFile = Server.MapPath("svg") + "\\" + ConfigurationManager.AppSettings["sourceFile"];
string targetFile = Server.MapPath("png") + "\\" + ConfigurationManager.AppSettings["targetFile"];
string args = "-f \"" + sourceFile + "\" -e \"" + targetFile + "\" -w100 -h40";
string inkscape = ConfigurationManager.AppSettings["inkscapeExe"];
// Generate the png via inkscape
ProcessStartInfo inkscapeInfo = new ProcessStartInfo(inkscape, args);
Process inkscape = Process.Start(inkscapeInfo);
inkscape.WaitForExit(5000);
runLiteral.Text = "Success!<br />" + args;
}
catch (Exception ex)
{
runLiteral.Text = ex.GetType().FullName + "<br />" + ex.Message + "<br />" + ex.StackTrace;
}
}
Can someone tell me what I'm doing wrong?
Thank you!
A couple things to check:
Make sure that the application pool identity for the web application (found in IIS, usually NetworkService) has permissions to execute inkscape.exe
If that is fine, check to make sure that the directory grants Modify permissions to the apppool identity on the directory(ies) you are writing the PNG to ("C:\inetpub\wwwroot\MyTest")
Alternatively, you can use impersonation to run the executable under a specific Windows account.

Categories

Resources