File.Copy Unauthorized access C# - c#

I've started to encounter a problem with File.Copy. This works fine for my data creation script, managing to duplicate thousands of files with no issues. My problem occurs when trying to create temp files later in my code.
I have added the code sample below that isn't working correctly. I've tried numerous different ways to try to resolve this to no avail. What I am doing is copying some user data files created in a directory on the C drive into a temp folder inside that user data folder.
Code
foreach (string originalFile in OriginalDataFileNames)
{
string tempFile = originalFile;
TempDataFiles.Add(tempFile);
Console.WriteLine("GlobalDataCtrl: Original Data File: " + XWSDataDirectory + "\\" + tempFile);
Console.WriteLine("GlobalDataCtrl: Saved Temp Data File: " + tempPath + "\\" + tempFile);
File.Copy(XWSDataDirectory + "\\" + originalFile, tempPath + "\\" + tempFile);
}
Exit Error
The program '[6256] XtremeWrestlingSim.vshost.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.
Any help is appreciated, thanks in advance!
SOLUTION:
FileStream outputFS = null;
FileStream inputFS = null;
outputFS = new FileStream(tempPath + "\\" + tempFile, FileMode.CreateNew, FileAccess.ReadWrite);
using (inputFS = new FileStream(XWSDataDirectory + "\\" + originalFile, FileMode.Open))
{
inputFS.CopyTo(outputFS);
}
outputFS.Close();
inputFS.Close();
Not sure how nicely formatted this is, but it works. Replace File.Copy with the above code.

You are using File.Create just before you call File.Copy, I think that is the issue, it is leaving an open stream.
Maybe removing the File.Create call will solve the issue. If not you could get the returned value (which is a stream) and close it before trying to copy.
The file is opened with read/write access and must be closed before it can be opened by another application.
See remarks https://msdn.microsoft.com/en-us/library/ms143361(v=vs.110).aspx

Related

I'm getting "The process cannot access the file because it is being used by another process" error. Any ideas?

So, I created a file and a txt file into the AppData, and I want to overwrite the txt. But when I try to do it, it keeps giving me that error. Any ideas?
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string setuppath = (path + "\\");
string nsetuppath = (setuppath + "newx" + "\\");
Directory.CreateDirectory(nsetuppath);
string hedef2 = (nsetuppath + "commands.txt");
File.Create(hedef2);
StreamWriter sw = new StreamWriter(hedef2); ----> This is where the error appears.
sw.WriteLine("Testtest");
Just use the using statement when using streams. The using statement automatically calls Dispose on the object when the code that is using it has completed.
//path to the file you want to create
string path = #"C:\code\Test.txt";
// Create the file, or overwrite if the file exists.
using (FileStream fs = File.Create(path))
{
byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
There are many ways of manipulate streams, keep it simple depending on your needs

Where should I close the File Stream?

I am trying to get the text of a file in C# (config) and to put it into fields. The code itself works, but I need to close the file stream to open a new one futher in the code, but don't know where to put the Close() tag. Can someone help me?
string documentspath = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string rpath = documentspath + #"\Luminous\remember.ini";
if (File.Exists(rpath))
{
try
{
string text = File.ReadAllText(rpath);
string path = text.Split('|')[0];
string process = text.Split('|')[1];
string loadLib = text.Split('|')[2];
pathBox.Text = path;
processBox.Text = process;
if (loadLib == "True")
{
loadLibrary.Checked = true;
}
else
{
manualMap.Checked = true;
}
} catch
{
MessageBox.Show("Config file corrupted. Remembered data has been lost and deleted.");
File.Delete(rpath);
}
} else
{
MessageBox.Show("No config file loaded. Welcome, " + Environment.UserName + ".");
}
EDIT: Here's the code from where I'm opening another file stream.
string documentspath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (!Directory.Exists(documentspath + #"\Luminous")) {
Directory.CreateDirectory(documentspath + #"\Luminous");
} else {
string rpath = documentspath + #"\Luminous\remember.ini";
if (!File.Exists(rpath)) {
File.Create(rpath);
File.WriteAllText(rpath, pathBox.Text + "|" + processBox.Text + "|" + loadlibcheck + "|" + manualmapcheck);
} else {
File.WriteAllText(rpath, string.Empty);
File.WriteAllText(rpath, pathBox.Text + "|" + processBox.Text + "|" + loadlibcheck + "|" + manualmapcheck);
}
}
You don't need to close File when using File.ReadAllText().
From MSDN:
File.ReadAllText Method Opens a text file, reads all lines of the file, and then closes the file.
Error appears because of this line:
File.Create(rpath);
Try:
using(File.Create(rpath)) {}
File.Create method actually return FileStream, which have to be disposed. Some info about it can be found on the documentation.
In section "Return Value":
Type: System.IO.FileStream A FileStream that provides read/write
access to the file specified in path.
In section "Remarks":
The FileStream object created by this method has a default FileShare value of None; no other process or code can access the created file until the original file handle is closed.
However, as Chris Dunaway mentioned, there is no need to use File.Create at all, as according to documentation WriteAllText :
Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.
You have two good options:
On the finally clause of your try/catch block.
Change your code to use using statement for your file stream.
You don't have to close anything. File.ReadAllText reads and closes a stream internally. So you can safely invoke another File.ReadAllText if you need to.

File copy process is being used,

I think this problem seems a lot to be questioned, but I still haven't found a solution.
I want to update my application by comparing dll. file version. I already got the file version, but when I want to update it, it gives me this error.
the code
FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(#"D:\Projects\Sample\bin\Debug\Sample.dll");
FileVersionInfo myFileVersionInfo2 = FileVersionInfo.GetVersionInfo(#"C:\Projects\Sample\bin\Debug\Sample.dll");
MessageBox.Show("Version number this: " + myFileVersionInfo.FileVersion);
MessageBox.Show("Version number 2: " + myFileVersionInfo2.FileVersion);
if (myFileVersionInfo.FileVersion != myFileVersionInfo2.FileVersion)
{
string file = "Sample.dll";
string source = #"D:\Projects\Sample\MainSystem\bin\Debug\";
string target = Application.StartupPath;//#C:\
string sourceFile = System.IO.Path.Combine(source, file);
string destFile = System.IO.Path.Combine(target, file);
System.IO.File.Copy(sourceFile, destFile, true); //<< error
{
the code run when I open the application and click button
the Error
System.IO.IOException: The process cannot access the file 'C:\Projects\Sample\bin\Debug\Sample.dll' because it is being used by another process.
The Class FileVersionInfo 'locks' the dll from which you are reading the fileversion. In order to access it again, try
myFileVersionInfo2 = default(FileVersionInfo);

How to make 2 process access the same path?

I'm using c#. I'm receiving an error about a path is currently accessed by other processes. What my system is trying to do is to access the path: #"C:\temps\" + client_ids + "_" + rown + ".pdf" and use the same path for attachment before sending it to client's email.
here's what I've done so far. I comment out some of my code because I'm not sure what to do.
FileStream fs = null;
using (fs = new FileStream(#"C:\\temps\\" + client_ids + "_" +
rown + ".pdf",
FileMode.Open,FileAccess.Read,FileShare.ReadWrite))
{
TextReader tr = new StreamReader(fs);
//report.ExportToDisk
//(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat,tr);
//report.Dispose();
//Attachment files = new Attachment(tr);
//Mailmsg.Attachments.Add(files);
//Clients.Send(Mailmsg);
}
you can make temp copy of file before you use it in mail attachment and then use the copy instead of the original file
You cannot attach a file to an email if that file is open. You must close (save) the file first.
While #ali answer is technically correct, it is unnecessary. Why go through the overhead of creating a copy of the file which then needs to be deleted, etc.?
Assuming I understand what you are trying to do correctly, simply move your code for mail to after the file is successfully created and saved. And, I don't think you need the overhead of either the filestream or the textreader. As long as your report object can save the file to disk someplace, you can attach that file to your email message and then send it.
While I do not claim to know anything about how Crystal Decisions handles exports, etc. Perhaps something like this would work:
(I got this code from: https://msdn.microsoft.com/en-us/library/ms226036(v=vs.90).aspx)
private void ExportToDisk (string fileName)
{
ExportOptions exportOpts = new ExportOptions();
DiskFileDestinationOptions diskOpts =
ExportOptions.CreateDiskFileDestinationOptions();
exportOpts.ExportFormatType = ExportFormatType.RichText;
exportOpts.ExportDestinationType =
ExportDestinationType.DiskFile;
diskOpts.DiskFileName = fileName;
exportOpts.ExportDestinationOptions = diskOpts;
Report.Export(exportOpts);
}
You will need to change the ExportFormatType property.
Then, simply attach the file to your email and send:
Attachment Files = new Attachment(filename);
Mailmsg.Attachments.add(files);
Clients.Send(Mailmsg);

Batch Image Manipulation keeps hanging

I use the following code to scale and crop all images in a folder.
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file);
string fileExtension = Path.GetExtension(file);
string filePath = Path.GetDirectoryName(file);
string newFileName = string.Empty;
long fileSize = new FileInfo(file).Length;
if (fileSize > fileSizeLimit)
{
string tempFile = System.IO.Path.GetTempFileName();
File.Copy(file, tempFile, true);
Bitmap sourceImage = (Bitmap)System.Drawing.Image.FromFile(tempFile);
System.Drawing.Image imgPhoto = ScaleCrop(sourceImage, sourceImage.Width / 4, sourceImage.Height / 4, AnchorPosition.Top);
Bitmap bitImage = new Bitmap(imgPhoto);
File.Delete(file);
newFileName = filePath + "\\" + fileNameWithoutExtension + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + CoilWarehouseProcessed + fileExtension;
bitImage.Save(newFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
imgPhoto.Dispose();
bitImage.Dispose();
}
If I run the application locally (in debug mode in VS2010) and point it to a network drive then all images are processed every time.
If I run it from a our local webserver the problem is that the app may process no images, it may process 5, it may process 1, it never does all of the images in a given folder, only ever some of them... then it hangs in the clients browser.
There are no events to view via the event log... the application does not crash or error in anyway... the fact that it will process an image proves it's not a permissions issue.
Any ideas why this is happening?
EDIT: Thanks to wazdev, but I ended up testing a less intrusive (and also don't like dependencies relying on 3rd party software) solution, and it all seems good so far... Basically I changed it so that when it copies the stream to produce a new image 'System.Drawing.Image imgPhoto = ...' to use a using statement to ensure that the 'temp' image is disposed of. I also moved the delete of the original (uncropped / unscaled image) file to be the last operation (In tests it has worked fine, only time will tell once more users come online and concurrency is tested):
string tempFile = System.IO.Path.GetTempFileName();
File.Copy(file, tempFile, true);
Bitmap sourceImage = (Bitmap)System.Drawing.Image.FromFile(tempFile);
System.Drawing.Image imgPhoto = ScaleCrop(sourceImage, sourceImage.Width / 4, sourceImage.Height / 4, AnchorPosition.Top);
Bitmap bitImage;
using (var bmpTemp = new Bitmap(imgPhoto))
{
bitImage = new Bitmap(bmpTemp);
}
newFileName = filePath + "\\" + fileNameWithoutExtension + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + CoilWarehouseProcessed + fileExtension;
bitImage.Save(newFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
imgPhoto.Dispose();
bitImage.Dispose();
File.Delete(file);
EDIT2: It's been live now for a few days and i've tested it every day and it is working well.. Here's all that I did;
Basically inside the ScaleCrop() call there was a GC.Collect and a Wait For Pending Finalisers() call. I removed the wait for pending call and moved the GC.Collect() to after the File.Delete().
I've seen this behaviour in the past when RAM was exhausted resulting in paging to disk. I've found a great deal of success in utilising the ImageResizing.net libraries:
http://imageresizing.net/
I updated my code to use this library and have never looked back.
HTH
(I have no affiliation with imageresizing.net - I'm just a very happy user)

Categories

Resources