I am writing code in Unity and I want to change file modification date. For that purpouse I am using File.SetLastWriteTime method. It works fine but on some android versions it simply does not work and gives me an erorr IOException: Invalid parameter. I googled tha and it seems to be some unresolved bug as number of people reported the same.
My question here is: is there another library or different way I can change last modification date using C#?
edit: I need to preserve original modification date. The file is being downloaded from Amazon S3 but the modification date is set to the exact moment that the file is being saved on the device. This is why after I downloaded it I tried to do File.SetLastWriteTime(file, original_date)
I'm seeing the same thing for some arbitrary files when using SetLastWriteTime via mono on a Synology NAS - some files failed with "Invalid parameter" IOExceptions.
After digging into it a bit, I found it was a permissions/ownership issue - running the same thing with sudo worked.
Check below code:
string fileName = #"E:\test.txt";
File.SetLastWriteTime(fileName, DateTime.Now);
I took snapshot for both the modified date. After and before run the code. You will check the given path again.
Related
I have a small problem that it took me sometime to found out what should i do.
The scenario goes like this.
I created a software that I already burn to a dvd-rw and installed an autorun to it.
When my software run. It will copy all files from that dvd and will paste it to the user designated drive. But I inserted a Flexible variable to some sort of textfile.
After the copying is complete. The next step of my software is to find all the Flexible variable and change it to its final output. for example the flexible varialble is <##COMPUTER_NAME> then my software will change it to its final data. But when i do it. my system in dvd-rw says access denied.
I also tried it in my Flash Drive and it works fine. But when i tried it in DVD it says access denied.
What should i do?
P.S. my system always running as administator.
Ok I found out that my problem is the dvd-r. Because it is nature that whenever you burn a file to it, the file will become read only always. So when my system try to write the specific file after copying it says denied due to the attribute of the file.
So I made a small changes in my installer.
I added to my code the function
Checking and changing the attribute of specific file
before executing the second command.
That's all and it work again perfectly. attribute is the key.
What I am trying to do is:
I have a Asp Mvc website. In it I need to create a function that does the following:
converts a .dwg (AutoCad) file to a .pdf file
converts a .dwg file to a .dwf file
I started with the, what I tought at the time, easier task to have the .dwg to a .pdf. After some research I found out that a way to do that (without buying a 3rd party license) is to install TrueView on the server and using it (or actually a AcCtrl.dll) to convert the file. Well, I did so. I installed the True View program and added a reference to AcCtrl Component (ACCTRLLib). Then I added a reference to the Dll inside the class file I am working on:
using ACCTRLLib;
So far so good. After that I followed the instructions on this post: PDF conversion using dwg true viewer in VB6
First, this is my code:
public static void ConvertFile()
{
IAcCtrl contrl = new AcCtrl();
contrl.PutSourcePath(#"D:\MMA\Autocat\File1.dwg");
string[] pdfPath = new string[1] { #"D:\MMA\Autocat\File1.dwg" };
contrl.SilentPublish(pdfPath);
}
Then, according to the post, I went to see if there is a registry with the specified path. The path that I have as a registry is the following:
HKEY_CURRENT_USER\Software\Autodesk\DWG TrueView\R13\DWGVIEWR-E001:409\Profiles\<<\Unnamed Profile>>\Dialogs\AcPublishDlg
So, I created a 'String Value' in it with ValueName: Location and ValueData: D:\MMA\Autocat\Testing
Alright. So, thats it. After all that I ran the application and called the function. The debugger goes through the code and everything executes (or at least looks like so) but nothing happens. I don't get a file in the D:\MMA\Autocat\Testing folder. I get no exception, no warning nothing. It just executes and nothing happens.
So what else did I tried. Some of those things might be a little naive or silly to try but nevertheless I did as I happen to be a little desperate.
I tried everything in a console application. I wasnt completely sure that this method is suitable for ASP MVC so I tried the same code with a console application unfortunatelly to the same result.
I added a file name inside the location string value. I changed the Location ValueData from 'D:\MMA\Autocat\Testing' to 'D:\MMA\Autocat\Testing\testFile.pdf' as I noticed that nowhere in the whole process a file name is asked for the converted file.
Following my thought from the previous conclusion I tried to supply the path to the 'result' file to the SilentPublish function.
string[] pdfPath = new string[1] { #"D:\MMA\Autocat\Testing\testFile.pdf" };
contrl.SilentPublish(pdfPath);
Again to no avail. So, my question, or rather questions are:
Is it possible to do it this way and if it is what am I doing wrong?
If its not than can you suggest a way?
Also if a .dwg to .dwf conversion is possible (with the same or different method I am all ears)
Thank you very much
True View does not expose the Autodesk.AutoCAD.PlottingServices namespace. You're going to need either a licensed copy of AutoCAD, RealDWG, or another third party API.
I'm not sure if you are still having this issue, but you should be able to do this with the Autodesk Forge API. Calls can be made from any language with a simple HTTP call. You will have to first convert to SVF and then to DXF and PDF from there.
I am building a wrapper for the Perforce .net API and I need to be able to download files from the Depot directly to my computer (not a check out!) as the file will be used in another project!
Currently I have a (quite special) solution where I run the "copy" command without submitting, copies that file to the correct location using the File.Copy function and then reverting the P4 copy. It works, but when the file is too big (~200 mb) I will get this error instead:
[Command time out[655371]: copy //FROM_PATH //TO_PATH]
I noticed that if you right click a file in the P4V you can choose the option "Export to...", I cannot find a command like that using the API or the command line... does anyone know a better way of exporting files than the one I currently use? (it needs to be able to run from a C# application)
Alternatively, if anyone knows how to bypass the Command time out, that would also be great!
Thanks!
Edit:
I found the solution to the Command time out:
You can change the delay until a time out occurs by creating a new instance of the TimeSpan class with the desired delay and then assigning it to the CommandTimeout variable of your connection instance (when you have a connection established):
example:
m_connection.CommandTimeout = TimeSpan.FromMilliseconds(milliseconds);
I am still interested to see if anyone knows a way of exporting files without having to do my strange version!
Are you looking for the 'p4 print -o' command?
I have read a similar post, but i just cant figure out the problem.
I have changed the windows permissions and changed routes.
When i try to save a file it throws me the exception:
Access to the path **** denied.
string route="D:\\";
FileStream fs = new FileStream(route, FileMode.Create); <--here is the problem
StreamWriter write = new StreamWriter(fs);
patient person = new patient();
patient.name = textBox1.Text;
patient.name2 = textBox2.Text;
You are trying to create a FileStream object for a directory (folder). Specify a file name (e.g. #"D:\test.txt") and the error will go away.
By the way, I would suggest that you use the StreamWriter constructor that takes an Encoding as its second parameter, because otherwise you might be in for an unpleasant surprise when trying to read the saved file later (using StreamReader).
Did you try specifing some file name?
eg:
string route="D:\\somefilename.txt";
tl;dr version: Make sure you are not trying to open a file marked in the file system as Read-Only in Read/Write mode.
I have come across this error in my travels trying to read in an XML file.
I have found that in some circumstances (detailed below) this error would be generated for a file even though the path and file name are correct.
File details:
The path and file name are valid, the file exists
Both the service account and the logged in user have Full Control permissions to the file and the full path
The file is marked as Read-Only
It is running on Windows Server 2008 R2
The path to the file was using local drive letters, not UNC path
When trying to read the file programmatically, the following behavior was observed while running the exact same code:
When running as the logged in user, the file is read with no error
When running as the service account, trying to read the file generates the Access Is Denied error with no details
In order to fix this, I had to change the method call from the default (Opening as RW) to opening the file as RO. Once I made that one change, it stopped throwing an error.
I had this issue for longer than I would like to admit.
I simply just needed to run VS as an administrator, rookie mistake on my part...
Hope this helps someone <3
If your problem persist with all those answers, try to change the file attribute to:
File.SetAttributes(yourfile, FileAttributes.Normal);
You do not have permissions to access the file.
Please be sure whether you can access the file in that drive.
string route= #"E:\Sample.text";
FileStream fs = new FileStream(route, FileMode.Create);
You have to provide the file name to create.
Please try this, now you can create.
TLDR : On my end, it had something to do with AVAST ! => Whitelist your application.
All of a sudden, I also got this UnauthorizedAccessException problem in the windows WPF program I'm writing. None of the solutions worked - except I couldn't figure out how to elevate my application to full privileges (not using VS) while at the same time, being already on the administrator account, I didn't feel the need to dig that deep in permission concerns.
The files are image files (jpg, psd, webp, etc.) I wasn't trying to open/write a directory, it has always been a valid path to a file, and I needed to write to the file, FileAccess.ReadWrite was inevitable. The files (and any of their parent directory) were not readonly (I even checked by code prior calling new FileStream(path, mode, access, share) via FileInfo.IsReadOnly) - so what happenned all of a sudden ???
Thinking about : I had an had drive crash, so I unpacked a backup of my solution code from another drive. In the meantime, I added codes in my application to PInvoke APIs to directly read hard drive sectors physical bytes as well as USB plug/unplug monitoring.
I started to get the Exception when I added those, but even though I temporarly removed the related codes from the application, I still got the UnauthorizedAccessException.
Then I remembered one thing I've done long ago, a painstaking similar issue where I wanted my application to communicate sensible data via Wifi, which was to add the executable among AVAST exceptions, and the assembly directory aswell (My app was already among the authorized apps through firewall)
Just did it for my application in AVAST settings, AND THE EXCEPTION IS GONE !!! Two whole days I'm lurking StackOverflow and the web to get moving on, FINALLY !
Details : I can't pinpoint exactly what AVAST didn't like in my application as the only changes I made :
Retrieved then launched the backup code - it worked like a charm, files (images) opens/write without problems (3 days ago)
Added USB detection (3 days ago - Just tested the code, didn't tried to open an image)
Added PInvoke physical drive direct read (2 days ago - FileStream, and the logic to define where/how to scan the damaged drive - Just tested the code, didn't tried to open an image)
Added image format detection starting from Jpg/Jfif.. 2 days ago, got the exception upon testing the code.
While searching for solutions, added an Image Gallery WPF UserControl to diplay pictures based on their signature and check which files gives the exception : almost all of them (some files opens/write okay - why ???)
Tried everything I've found on SO (since the last 2 days) until I opened AVAST settings and whitelist my application.
... now I can move on into adding a bunch of file signatures to retrieve as many datas as I could.
If this may help those who like me, aren't failing on the "I'm passing a directory path instead that of a file", yet, have no time to learn exactly why antiviruses think our own code is a malware.
Just Using the below worked for me on OSX.
var path = "TempForTest";
I have a Visual Studio 2008 solution that when I build, returns the following error:
Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. Parameter name: ticks
There is no reference file/line/column in the error. Becoming quite frustrating as the solution builds in the end, however I cannot debug.
In the solution, there is no reference/using to DateTime.MinValue.Ticks at all...
EDIT:
There are only 2 references to ticks in my solution, and neither of those are causing the issue.
There are no constructor issues with DateTime either. I do not believe it is related to DateTime or ticks at all.
I have found a similar reference where it turned out to be a corrupt image file in the solution, where it did not have a date-modified value. This is not affecting mine though.
The error message comes from the DateTime constructor that takes a long value.
Somewhere in your code you are creating a DateTime value from a constant value, so that the actual creation is done at compile time rather than runtime. That value is out of range for the constructor.
Code like this gives the same exception:
public static DateTime startTime = new DateTime(-1);
However, the exception happens at run time. To get it at compile time there is probably something in your markup code, like an argument value in a server control.
It sounds like you're not initialising ticks.
In that case there won't be a reference to DateTime.MinValue.Ticks.
You'll have to search for ticks and double check all occurrences.
I don't know if you have ever solved this but I have found that it has occurred due to glitches in the meta data for images.
It has happened in two different projects, created by two different systems, but the culprit has been missing file modified data for images.
I have written a post about this:
http://runtingsproper.blogspot.com/2010/03/solved-error-1-ticks-must-be-between.html
But the basic crux is of the article is to do the following:
Switch to the Output tab and see what the last folder was which was compiled before it borked.
Delete that folder.
Recompile
Keep repeating steps 1 to 3 until the site compiles
When it compiles start bringing back individual files in the last batch you deleted until you find the offending file
This is a simple looking error message that is deceptively hard to track down.
it's a compile time error. i also got this error. To Get rid this error ....i opened my file
proximanova-black.ttf in notepad and saveas . this resolved my error.
this article should get you back on track without spending hours scratching your head.
http://runtingsproper.blogspot.com/2010/03/solved-error-1-ticks-must-be-between.html
I solved the problem with rtpHarrys answear. It turned out I had a .ttf-file with the wrong date format so I just resaved it, and now it works!
I experienced the same error in Visual Studio 2013. As you said there were no Ticks or something like that. But accidently some time ago I had a calendar problem on my computer that the date was 2199 or something like that and I realized that after that error I haven't run the code. The solution for me was:
Copying local git repo to usb flash disc.
Deleting the local git repo.
Copy & paste the git repo to disc again from usb disc.