Windows service acces denied to path 192.168.1.XX - c#

My windows service copy a file from antoher PC in the same network.
This is the code
string fileName = "test.txt";
string sourcePath = #"\\192.168.1.XX\Users\utente\Desktop\prova\";
string targetPath = #"C:\Users\IDEA\source\repos\BotterService\bin\Debug";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.Directory.CreateDirectory(targetPath);
try
{
File.Copy(sourceFile, destFile);
}
catch (Exception ex)
{
Debug("EXCEPTION: " + ex.Message);
}
From my PC I have all permiss to read and write test.txt file. Infact I can read, write and save this file.
I got the error permission denied to 192.168.1.XX\Users\utente\Desktop\prova\
Thanks

Related

save chart image on file server

public void SaveFileOnDisk(Chart ms, string FileName)
{
try
{
string appPath = HttpContext.Current.Request.ApplicationPath;
string physicalPath = HttpContext.Current.Request.MapPath(appPath);
string strpath = physicalPath + "\\Images";
string WorkingDirectory = strpath;
ms.SaveImage("\\\\servername\\Testing" + "\\" + FileName + System.DateTime.Today.ToString("ddmmyyyy") + ".png", ChartImageFormat.Png);
}
i want to save image on file server folder getting error Access to the path '\SERVERNAME\Testing\test09002016.png' is denied.
i have credentials for file server, but how to use them while saving image?

How to Copy URI path file into temp folder?

I have a file path in URI and trying to copy the URI file into C:\temp\ but I am getting an error "Could not find file (from the URI path)" If anyone suggest me would be a great helpful. Thank you.
public String getFile(String uri)
{
// Download file in temp folder
String fileName = Path.GetFileName(uri.Replace("/", "\\"));
Uri fileUri = new Uri(uri);
string fullFilePath = absoluteUri.AbsoluteUri.ToString();
string localPath = new Uri(fullFilePath).LocalPath;
String tempFolder = #"C:\temp\";
File.Copy(localPath, tempFolder);
return fileName;
}
You can use web client to download a file from a Uri
new WebClient().DownloadFile(uri, Path.Combine(filePath, fileName));
I tried in a different way and its working for me. I hope this could help for someone else.
private String getFile(String uri)
{
Uri uriFile = new Uri(uri);
String fileName = Path.GetFileName(uri);
List<String> fileData = new List<String>();
// Reads all the code lines of a file
fileData = readCodeLines(uriFile);
String tempPath = #"c:\temp\";
try
{
if (!Directory.Exists(tempPath))
{
Directory.CreateDirectory(tempPath);
}
File.WriteAllLines(tempPath, fileData);
}
catch (IOException ex)
{
MessageBox.Show("Could not find the Temp folder" + " " + tempPath);
}
return fileName;
}

How to copy file from one dir to another under IIS root using asp.net web service?

I am developing asp.net web service using win7 IIs server.under IIs root dir there are two sub dir how can i copy file from one sub folder to another this my try:
string fileName="file.txt";
string sourcePath = #"localhost\C:\inetpub\wwwroot\source";
string targetPath = #"localhost\C:\inetpub\wwwroot\dest";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(sourceFile, destFile, true);
thanks in advance
You should use physical paths. That is :
string fileName="file.txt";
string sourcePath = #"C:\inetpub\wwwroot\source";
string targetPath = #"C:\inetpub\wwwroot\dest";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(sourceFile, destFile, true);
If you need to involve the website root (in this case, folders on the site root folder), it should be like this:
string fileName="file.txt";
string sourcePath = Server.MapPath("/source");
string targetPath = Server.MapPath("/dest");
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(sourceFile, destFile, true);
Best

Move files from one folder to another C# Error File being used by another process

I tired to copy files containing a particular string from one folder to another but it keeps giving me the error message System.IO.IOException: The process cannot access the file 'Z:\Upload\Text-File-1.txt' because it is being used by another process. Have tried a few things but nothing works. Not sure how to fix it.
using System;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
CheckandMoveFiles();
}
private static void CheckandMoveFiles()
{
//put filenames in array
string[] sourcePath = Directory.GetFiles(#"Z:\Upload\");
string targetPath = #"Z:\Upload\TextFiles\";
try
{
//Get each filepath and check for string
foreach (string name in sourcePath)
{
string d = "Text";
if (name.Contains(d))
{
string fileName = name;
string sourceFile = System.IO.Path.Combine(sourcePath.ToString(), fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
using (StreamReader reader = new StreamReader(sourceFile))
{
File.Copy(sourceFile, destFile, true);
}
}
}
}
catch (IOException ex)
{
Console.WriteLine(ex); // Write error
}
Console.WriteLine("****************************DONE***************************************");
Console.ReadLine();
}
}
}
It looks like your problem is in the following lines:
string sourceFile = System.IO.Path.Combine(sourcePath.ToString(), fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
because fileName already contains the full path, and sourcePath is a string array, so sourcePath.ToString() would not yield the path you expect.
Instead try:
string sourceFile = fileName;
string destFile = System.IO.Path.Combine(targetPath, System.IO.Path.GetFileName(fileName));
using (StreamReader reader = new StreamReader(sourceFile))
{
File.Copy(sourceFile, destFile, true);
}
You're opening a StreamReader to read the sourceFile, and then you try to File.Copy() it? that's why you're getting the error
Lose the StreamReader, you don't need it
File.Copy(sourceFile, destFile, true);
Will do
Should be like this:
if (name.Contains(d))
{
string fileName = name;
string sourceFile = System.IO.Path.Combine(sourcePath.ToString(), fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
File.Copy(sourceFile, destFile, true);
}
File.Copy takes file path as parameter not the stream that points to the file.
Try This Friend...
public class SimpleFileCopy
{
static void Main()
{
string fileName = "test.txt";
string sourcePath = #"C:\Users\Public\TestFolder";
string targetPath = #"C:\Users\Public\TestFolder\SubDir";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
System.IO.File.Copy(sourceFile, destFile, true);
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);
foreach (string s in files)
{
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile, true);
}
}
else
{
Console.WriteLine("Source path does not exist!");
}
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}

Copy file to directory

Is there no .NET library call for copying a file to a directory? All library calls I found (e.g. File.Copy() or FileInfo.CopyTo()) do only support copying a file to another fully specified file.
string file = "C:\Dir\ect\ory\file.txt";
string dir = "C:\Other\Directory";
File.Copy(file, dir); // does not work, requires filename
Is there a library call? If no, what's the best way to write my own utility, do I really have to use Path.GetFileName()?
do I really have to use Path.GetFileName()?
Exactly:
string destination = Path.Combine(dir, Path.GetFileName(file));
Directory.CreateDirectory(dir);
File.Copy(file, destination);
Try this example
public class SimpleFileCopy
{
static void Main()
{
string fileName = "test.txt";
string sourcePath = #"C:\Users\Public\TestFolder";
string targetPath = #"C:\Users\Public\TestFolder\SubDir";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(sourceFile, destFile, true);
}
}

Categories

Resources